|
| 1 | +--- |
| 2 | +title: 3019.按键变更的次数 |
| 3 | +date: 2025-01-07 13:14:51 |
| 4 | +tags: [题解, LeetCode, 简单, 字符串] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】3019.按键变更的次数:遍历(转小写) |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/number-of-changing-keys/](https://leetcode.cn/problems/number-of-changing-keys/) |
| 10 | + |
| 11 | +<p>给你一个下标从<strong> 0</strong> 开始的字符串 <code>s</code> ,该字符串由用户输入。按键变更的定义是:使用与上次使用的按键不同的键。例如 <code>s = "ab"</code> 表示按键变更一次,而 <code>s = "bBBb"</code> 不存在按键变更。</p> |
| 12 | + |
| 13 | +<p>返回用户输入过程中按键变更的次数。</p> |
| 14 | + |
| 15 | +<p><strong>注意:</strong><code>shift</code> 或 <code>caps lock</code> 等修饰键不计入按键变更,也就是说,如果用户先输入字母 <code>'a'</code> 然后输入字母 <code>'A'</code> ,不算作按键变更。</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p><strong class="example">示例 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>输入:</strong>s = "aAbBcC" |
| 23 | +<strong>输出:</strong>2 |
| 24 | +<strong>解释:</strong> |
| 25 | +从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。 |
| 26 | +从 s[1] = 'A' 到 s[2] = 'b',按键变更。 |
| 27 | +从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。 |
| 28 | +从 s[3] = 'B' 到 s[4] = 'c',按键变更。 |
| 29 | +从 s[4] = 'c' 到 s[5] = 'C',不存在按键变更,因为不计入 caps lock 或 shift 。 |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">示例 2:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>输入:</strong>s = "AaAaAaaA" |
| 36 | +<strong>输出:</strong>0 |
| 37 | +<strong>解释:</strong> 不存在按键变更,因为这个过程中只按下字母 'a' 和 'A' ,不需要进行按键变更。<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 --> |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | + |
| 42 | +<p><strong>提示:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>1 <= s.length <= 100</code></li> |
| 46 | + <li><code>s</code> 仅由英文大写字母和小写字母组成。</li> |
| 47 | +</ul> |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +## 解题方法:遍历 |
| 52 | + |
| 53 | +从第二个字符开始遍历字符串,如果当前字符串的小写和前一个字符的小写不相同,则答案数量加一。 |
| 54 | + |
| 55 | ++ 时间复杂度$O(len(s))$ |
| 56 | ++ 空间复杂度$O(1)$ |
| 57 | + |
| 58 | +### AC代码 |
| 59 | + |
| 60 | +#### C++ |
| 61 | + |
| 62 | +```cpp |
| 63 | +/* |
| 64 | + * @Author: LetMeFly |
| 65 | + * @Date: 2025-01-07 13:03:56 |
| 66 | + * @LastEditors: LetMeFly.xyz |
| 67 | + * @LastEditTime: 2025-01-07 13:08:37 |
| 68 | + */ |
| 69 | +class Solution { |
| 70 | +public: |
| 71 | + int countKeyChanges(string& s) { |
| 72 | + int ans = 0; |
| 73 | + for (int i = 1; i < s.size(); i++) { |
| 74 | + ans += tolower(s[i]) != tolower(s[i - 1]); |
| 75 | + } |
| 76 | + return ans; |
| 77 | + } |
| 78 | +}; |
| 79 | +``` |
| 80 | +
|
| 81 | +#### Python |
| 82 | +
|
| 83 | +```python |
| 84 | +''' |
| 85 | +Author: LetMeFly |
| 86 | +Date: 2025-01-07 13:09:14 |
| 87 | +LastEditors: LetMeFly.xyz |
| 88 | +LastEditTime: 2025-01-07 13:09:43 |
| 89 | +''' |
| 90 | +class Solution: |
| 91 | + def countKeyChanges(self, s: str) -> int: |
| 92 | + return sum(s[i].lower() != s[i - 1].lower() for i in range(1, len(s))) |
| 93 | +``` |
| 94 | + |
| 95 | +#### Java |
| 96 | + |
| 97 | +```java |
| 98 | +/* |
| 99 | + * @Author: LetMeFly |
| 100 | + * @Date: 2025-01-07 13:10:05 |
| 101 | + * @LastEditors: LetMeFly.xyz |
| 102 | + * @LastEditTime: 2025-01-07 13:11:25 |
| 103 | + */ |
| 104 | +class Solution { |
| 105 | + public int countKeyChanges(String s) { |
| 106 | + int ans = 0; |
| 107 | + for (int i = 1; i < s.length(); i++) { |
| 108 | + if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) { |
| 109 | + ans++; |
| 110 | + } |
| 111 | + } |
| 112 | + return ans; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### Go |
| 118 | + |
| 119 | +```go |
| 120 | +/* |
| 121 | + * @Author: LetMeFly |
| 122 | + * @Date: 2025-01-07 13:11:57 |
| 123 | + * @LastEditors: LetMeFly.xyz |
| 124 | + * @LastEditTime: 2025-01-07 13:13:25 |
| 125 | + */ |
| 126 | +package main |
| 127 | +import "strings" |
| 128 | + |
| 129 | +func countKeyChanges(s string) (ans int) { |
| 130 | + for i := 1; i < len(s); i++ { |
| 131 | + if strings.ToLower(string(s[i])) != strings.ToLower(string(s[i - 1])) { |
| 132 | + ans++ |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/01/07/LeetCode%203019.%E6%8C%89%E9%94%AE%E5%8F%98%E6%9B%B4%E7%9A%84%E6%AC%A1%E6%95%B0/)哦~ |
| 140 | +> |
| 141 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/144983704](https://letmefly.blog.csdn.net/article/details/144983704) |
0 commit comments