其他分享
首页 > 其他分享> > LeetCode 12.1每日一题

LeetCode 12.1每日一题

作者:互联网

1446. 连续字符

给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。

请你返回字符串的能量。

很难说怎么写,简单遍历即可

class Solution:
    def maxPower(self, s: str) -> int:
        ans, cnt = 1, 1
        for i in range(1, len(s)):
            if s[i] == s[i - 1]:
                cnt += 1
            else:
                cnt = 1
            ans = max(ans, cnt)
        return ans

标签:字符,cnt,int,每日,12.1,ans,字符串,能量,LeetCode
来源: https://blog.csdn.net/m0_51630248/article/details/121652947