Skip to content

Commit a53878f

Browse files
committed
solve problem Remove Duplicates From Sorted Array II
1 parent fced454 commit a53878f

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ All solutions will be accepted!
130130
|796|[Rotate String](https://leetcode-cn.com/problems/rotate-string/description/)|[java/py/js](./algorithms/RotateString)|Easy|
131131
|844|[Backspace String Compare](https://leetcode-cn.com/problems/backspace-string-compare/description/)|[java/py/js](./algorithms/BackspaceStringCompare)|Easy|
132132
|26|[Remove Duplicates From Sorted Array](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedArray)|Easy|
133+
|80|[Remove Duplicates From Sorted Array II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedArrayII)|Medium|
133134
|112|[Path Sum](https://leetcode-cn.com/problems/path-sum/description/)|[java/py/js](./algorithms/PathSum)|Easy|
134135
|113|[Path Sum II](https://leetcode-cn.com/problems/path-sum-ii/description/)|[java/py/js](./algorithms/PathSumII)|Medium|
135136
|437|[Path Sum III](https://leetcode-cn.com/problems/path-sum-iii/description/)|[java/py/js](./algorithms/PathSumIII)|Easy|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Remove Duplicates From Sorted Array II
2+
We can solve this problem by double pointers
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int length = nums.length;
4+
if (length == 0)
5+
return 0;
6+
7+
int count = 0,
8+
pre = 0;
9+
10+
for (int i = 1; i < length; i++) {
11+
if (nums[i] == nums[pre]) {
12+
count++;
13+
if (count < 2)
14+
nums[++pre] = nums[i];
15+
} else {
16+
nums[++pre] = nums[i];
17+
count = 0;
18+
}
19+
}
20+
21+
return pre + 1;
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var removeDuplicates = function(nums) {
6+
let length = nums.length
7+
if (length == 0)
8+
return 0
9+
10+
let count = 0,
11+
pre = 0
12+
13+
for (let i = 1; i < length; i++) {
14+
if (nums[i] == nums[pre]) {
15+
count++
16+
if (count < 2)
17+
nums[++pre] = nums[i]
18+
} else {
19+
nums[++pre] = nums[i]
20+
count = 0
21+
}
22+
}
23+
24+
return pre + 1
25+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def removeDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if len(nums) == 0:
8+
return 0
9+
10+
count = 0
11+
pre = 0
12+
13+
for i in xrange(1, len(nums)):
14+
if nums[pre] == nums[i]:
15+
count += 1
16+
if count < 2:
17+
pre += 1
18+
nums[pre] = nums[i]
19+
else:
20+
pre += 1
21+
nums[pre] = nums[i]
22+
count = 0
23+
24+
return pre + 1
25+

0 commit comments

Comments
 (0)