1446.cpp 965 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: zhuyijun
  5. * @Date: 2021-12-21 09:57:26
  6. * @LastEditTime: 2021-12-21 10:18:55
  7. */
  8. /**
  9. * 1446. 连续字符
  10. * 给你一个字符串
  11. * s,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。请你返回字符串的长度
  12. *
  13. * 输入:s = "leetcode"
  14. * 输出:2
  15. * 解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。
  16. *
  17. * 输入:s = "triplepillooooow"
  18. * 输出:5
  19. */
  20. #include <algorithm>
  21. #include <iostream>
  22. #include <vector>
  23. using namespace std;
  24. int maxPower(string s) {
  25. char temp;
  26. vector<int> list;
  27. int i = 0;
  28. for (char m : s) {
  29. if (temp == m) {
  30. ++i;
  31. } else {
  32. list.push_back(i);
  33. i = 1;
  34. temp = m;
  35. }
  36. }
  37. list.push_back(i);
  38. list.get_allocator();
  39. auto p = max_element(list.begin(), list.end());
  40. return list[p - list.begin()];
  41. }
  42. int main() {
  43. cout << maxPower("aaabbbbbdddccccccccdk");
  44. return 0;
  45. }