给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
请你返回字符串的能量。
class Solution {
public:
int maxPower(string s) {
int max_str=1;
int count_str=1;
for(int i=1;i<s.length();i++)
{
if(s[i]==s[i-1])
count_str++;
else
{
if(max_str<count_str)
max_str=count_str;
count_str=1;
}
}
if(max_str<count_str)
max_str=count_str;
return max_str;
}
};
执行结果: 通过
执行用时:4 ms, 在所有 C++ 提交中击败了79.78%的用户
内存消耗:6.6 MB, 在所有 C++ 提交中击败了89.53%的用户
通过测试用例:333 / 333