分组循环
# 2765. 最长交替子序列 (opens new window)
见灵*直播讲解;
1
# 228. 汇总区间 (opens new window)
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
int start = 0;
for(int i=0;i<nums.length;i++){
if(i+1==nums.length || nums[i+1]!=nums[i]+1){
if(start==i)
res.add(String.valueOf(nums[start]));
else
res.add(String.format("%d->%d",nums[start],nums[i]));
start = i+1;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1446. 连续字符 (opens new window)
class Solution {
public int maxPower(String s) {
int n=s.length();
int i=1,ans=1;
while(i<n){
if(s.charAt(i)!=s.charAt(i-1)){
i++;
continue;
}
int start = i;
i++;
while(i<n && s.charAt(i)==s.charAt(i-1)){
i++;
}
ans = Math.max(ans,i-start+1);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19