LC3019. 按键变更的次数

题目链接:3019. 按键变更的次数 - 力扣(LeetCode)

题解

  • 按题意模拟,看其他人题解有用位运算去写更加简洁。

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# @lc app=leetcode.cn id=3019 lang=python3
#
# [3019] 按键变更的次数
#

# @lc code=start
class Solution:
def countKeyChanges(self, s: str) -> int:
ans = 0
s = s.lower()
for i in range(1, len(s)):
if s[i] != s[i-1]:
ans += 1
return ans
# @lc code=end

作者

Ryen Xiang

发布于

2025-01-12

更新于

2025-01-12

许可协议


网络回响

评论