1446. 连续字符

Leetcode题目:摸鱼题

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

请你返回字符串 s 的 能量。

示例 1:

输入:s = "leetcode"
输出:2
解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。

示例 2:

输入:s = "abbcccddddeeeeedcba"
输出:5
解释:子字符串 "eeeee" 长度为 5 ,只包含字符 'e' 。

提示:

  • 1 <= s.length <= 500
  • s 只包含小写英文字母。

解题思路

摸鱼题

摸个鱼先,不想刷新题那就刷点简单题保持下手感了。

class Solution {
public:
    int maxPower(string s) {
        int result=1;
        int tmp=1;
        int len=s.length();
        char cur=s[0];
        for(int i=1;i<len;i++)
        {
            
            if(cur==s[i])
            {
                tmp++;
                if(result<tmp)
                    result=tmp;
            }else{
                tmp=1;
                cur=s[i];
            }
        }
        return result;
    }
};
Subscribe
提醒
0 评论
Inline Feedbacks
View all comments
0
在此留下你的评论x