- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
- 80. Remove Duplicates from Sorte
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
![](https://img.haomeiwen.com/i1560080/e510b069761a3beb.png)
(图片来源https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
)
日期 | 是否一次通过 | comment |
---|---|---|
2020-03-10 | 0 |
public int removeDuplicates2(int[] nums) {
if(nums.length < 2) {
return nums.length;
}
int idx = 2;
for(int i=2; i<nums.length; i++) {
if(nums[i] > nums[idx-2]) {
nums[idx++] = nums[i];
}
}
return idx;
}
public int removeDuplicates(int[] nums) {
int i = 0;
for(int v : nums) {
if(i < 2 || v > nums[i-2]) {
nums[i++] = v;
}
}
return i;
}
只要每个元素出现一次:
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
public int removeDuplicates1(int[] nums) {
if(nums.length < 1) {
return nums.length;
}
int idx = 1;
for(int i=1; i<nums.length; i++) {
if(nums[i] > nums[idx - 1]) {
nums[idx++] = nums[i];
}
}
return idx;
}
网友评论