1446.连续字符
链接:1446.连续字符
难度:Easy
标签:字符串
简介:给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
题解 1 - typescript
- 编辑时间:2021-12-01
- 执行用时:4ms
- 内存消耗:5.9MB
- 编程语言:typescript
- 解法介绍:遍历。
int maxPower(char * s){
    int ans = 0;
    for (int i = 0; i < strlen(s); i++) {
        int cnt = 1;
        char ch = s[i];
        while (i + 1 < strlen(s) && s[i + 1] == ch) {
            i++;
            cnt++;
        }
        if (cnt > ans) ans = cnt;
    }
    return ans;
}
题解 2 - typescript
- 编辑时间:2021-12-01
- 执行用时:88ms
- 内存消耗:39.9MB
- 编程语言:typescript
- 解法介绍:遍历。
function maxPower(s: string): number {
  let ans = 0;
  for (let i = 0, n = s.length; i < n; i++) {
    let cnt = 1;
    const ch = s[i];
    while (i + 1 < n && s[i + 1] === ch) {
      i++;
      cnt++;
    }
    ans = Math.max(ans, cnt);
  }
  return ans;
}