123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /*
- * @Description:
- * @Version: 1.0
- * @Autor: zhuyijun
- * @Date: 2021-12-21 09:57:26
- * @LastEditTime: 2021-12-21 10:18:55
- */
- /**
- * 1446. 连续字符
- * 给你一个字符串
- * s,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。请你返回字符串的长度
- *
- * 输入:s = "leetcode"
- * 输出:2
- * 解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。
- *
- * 输入:s = "triplepillooooow"
- * 输出:5
- */
- #include <algorithm>
- #include <iostream>
- #include <vector>
- using namespace std;
- int maxPower(string s) {
- char temp;
- vector<int> list;
- int i = 0;
- for (char m : s) {
- if (temp == m) {
- ++i;
- } else {
- list.push_back(i);
- i = 1;
- temp = m;
- }
- }
- list.push_back(i);
- list.get_allocator();
- auto p = max_element(list.begin(), list.end());
- return list[p - list.begin()];
- }
- int main() {
- cout << maxPower("aaabbbbbdddccccccccdk");
- return 0;
- }
|