26. 删除有序数组中的重复项
# 26. 删除有序数组中的重复项 (opens new window)
解题思路:双指针
class Solution {
public int removeDuplicates(int[] nums) {
int slow=0;
int fast=0;
while(fast<nums.length){
while(fast+1<nums.length&&nums[fast]==nums[fast+1]) fast++;
nums[slow]=nums[fast];
slow++;
fast++;
}
return slow;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
编辑 (opens new window)
上次更新: 2023/12/15, 15:49:57