Skip to content

Commit 0a60fbd

Browse files
authored
Merge pull request #694 from LetMeFly666/3019
添加问题“3019.按键变更的次数”的代码和题解
2 parents 226ccba + 968b25a commit 0a60fbd

8 files changed

+220
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-07 13:03:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-07 13:08:37
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countKeyChanges(string& s) {
14+
int ans = 0;
15+
for (int i = 1; i < s.size(); i++) {
16+
ans += tolower(s[i]) != tolower(s[i - 1]);
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-07 13:11:57
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-07 13:13:25
6+
*/
7+
package main
8+
import "strings"
9+
10+
func countKeyChanges(s string) (ans int) {
11+
for i := 1; i < len(s); i++ {
12+
if strings.ToLower(string(s[i])) != strings.ToLower(string(s[i - 1])) {
13+
ans++
14+
}
15+
}
16+
return
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-07 13:10:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-07 13:11:25
6+
*/
7+
class Solution {
8+
public int countKeyChanges(String s) {
9+
int ans = 0;
10+
for (int i = 1; i < s.length(); i++) {
11+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
12+
ans++;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-01-07 13:09:14
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-01-07 13:09:43
6+
'''
7+
class Solution:
8+
def countKeyChanges(self, s: str) -> int:
9+
return sum(s[i].lower() != s[i - 1].lower() for i in range(1, len(s)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
|2974.最小数字游戏|简单|<a href="https://leetcode.cn/problems/minimum-number-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/12/LeetCode%202974.%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AD%97%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140365205" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-game/solutions/2840437/letmefly-2974zui-xiao-shu-zi-you-xi-pai-ib5em/" target="_blank">LeetCode题解</a>|
697697
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
698698
|3011.判断一个数组是否可以变为有序|中等|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/13/LeetCode%203011.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%98%E4%B8%BA%E6%9C%89%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140391465" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/solutions/2841665/letmefly-3011pan-duan-yi-ge-shu-zu-shi-f-i9ck/" target="_blank">LeetCode题解</a>|
699+
|3019.按键变更的次数|简单|<a href="https://leetcode.cn/problems/number-of-changing-keys/solutions/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144983704" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-changing-keys/solutions/3040949/letmefly-3019an-jian-bian-geng-de-ci-shu-pgzx/" target="_blank">LeetCode题解</a>|
699700
|3033.修改矩阵|简单|<a href="https://leetcode.cn/problems/modify-the-matrix/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/05/LeetCode%203033.%E4%BF%AE%E6%94%B9%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140219034" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/modify-the-matrix/solutions/2832430/letmefly-3033xiu-gai-ju-zhen-yuan-di-xiu-e0cy/" target="_blank">LeetCode题解</a>|
700701
|3038.相同分数的最大操作数目I|简单|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/07/LeetCode%203038.%E7%9B%B8%E5%90%8C%E5%88%86%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E6%93%8D%E4%BD%9C%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139535702" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/solutions/2804245/letmefly-3038xiang-tong-fen-shu-de-zui-d-dkj7/" target="_blank">LeetCode题解</a>|
701702
|3046.分割数组|简单|<a href="https://leetcode.cn/problems/split-the-array/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/28/LeetCode%203046.%E5%88%86%E5%89%B2%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144789679" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-the-array/solutions/3032725/letmefly-3046fen-ge-shu-zu-ji-shu-by-tis-33vv/" target="_blank">LeetCode题解</a>|
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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>&nbsp;</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>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= s.length &lt;= 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)

Solutions/Other-Accumulation-Messy.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ q.pop();
6767

6868
有个**投机取巧但不值得做**的办法:删除“当前设备”并重新备份,系统会首先备份其他数据(正好是你想要的),最后备份“应用列表”。备份应用列表的过程中空间满了,备份终止,但你想备份的数据也已经备份完了。
6969

70+
## 安卓应用独占蓝牙信道
71+
72+
使用蓝牙耳机进行腾讯系列软件通话时(包括但不限于腾讯会议、微信视频),软件会独占蓝牙信道,使得:
73+
74+
1. 蓝牙无法同时播放音乐
75+
2. 蓝牙声音奇大无比震耳欲聋,调到最小也很大,想调到0调不到
76+
77+
电脑上腾讯系软件也这样,但是可用通过`在声音图标上右键 -> 声音 -> 播放 -> 双击蓝牙设备 -> 高级 -> 取消勾选“允许应用程序独占控制该设备”`解决。
78+
79+
安卓上暂未找到可行办法。。。emm,我说你个tx,不给用户一个选项么,上来就独占,如果我不想让你独占呢。。。
80+
81+
还有使用腾讯会议时经常弹出的“监测到音量过小,建议调整到50%以上”。好家伙,最低音量(6%)我都觉得震耳欲聋,你这样是想让用户报工伤吗?
82+
7083
## 司法人工智能
7184

7285
### Large Legal Fictions: Profiling LegalHallucinations in Large Language Models

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ tags: [其他, 知识, 英语, Notes]
875875
|butchery|n. 残杀,杀戮,屠宰工作|
876876
|||
877877
|secretariat|n. 秘书处,书记处|
878-
|sheriff|n. 县(郡)治安官|
878+
|<font color="#28bea0" title="二次复习">sheriff</font>|n. 县(郡)治安官|
879879
|inasmuch|adv. 由于,〈古〉只要<details><summary>例句</summary>Inasmuch as you are an adult, you must be responsible for your own behavior.<br/>因为你是一个成年人,你必须对自己的行为负责。</details>|
880880
|countenance|n. 面容,脸色,面部表情<br/>v. 支持,赞成,同意|
881881
|||
@@ -927,6 +927,7 @@ tags: [其他, 知识, 英语, Notes]
927927
|||
928928
|snore|n. 鼾声<br/>v. 打鼾|
929929
|lark|n. 云雀,百灵鸟,玩乐,嬉戏<br/>v. 捉云雀,嬉戏,骑马越野,愚弄|
930+
|||
930931

931932
<p class="wordCounts">单词收录总数</p>
932933

0 commit comments

Comments
 (0)