1446. Consecutive Characters 链接到标题 判断最长连续相同字符的长度,记录下当前字母,然后比较与 flag 是否相同,如果相同则 +1,不相同则重置为 1。
也可以通过 itertools.groupby 来实现(新学习的。。)
class Solution: def maxPower(self, s: str) -> int: if len(s) == 1: return 1 tmp = s[0] cnt = 1 max_cnt = 0 for i in s[1:]: if i == tmp: cnt += 1 else: tmp = i cnt = 1 max_cnt = max(max_cnt, cnt) return max_cnt 1447. Simplified Fractions 链接到标题 遍历分子与分母,求最大公约数,如果最大公约数为1,则将结果保存下来。分母从 2 开始计算。
func simplifiedFractions(n int) []string { ret := []string{} for i := 2; i <= n; i++ { for j := 1; j < i; j++ { if gcd(i, j) == 1 { ret = append(ret, fmt.