Re: [闲聊] 每日leetcode

楼主: dont   2024-11-05 18:23:06
2914. Minimum Number of Changes to Make Binary String Beautiful
## 思路
扫整个字串 并纪录1,0的奇偶
如果目前字符是1/0 然后有奇数个0/1 就reset并且result+1
不然就更新1,0的奇偶
## Code
```python
class Solution:
def minChanges(self, s: str) -> int:
one = zero = 0
res = 0
for ch in s:
if (ch == '1' and zero) or (ch == '0' and one):
one = zero = 0
res += 1
elif ch == '1':
one ^= 1
else:
zero ^= 1
return res
```
作者: Meaverzt (Meaverzt)   2024-11-05 18:26:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com