File tree Expand file tree Collapse file tree 5 files changed +76
-0
lines changed
algorithms/RemoveDuplicatesFromSortedArrayII Expand file tree Collapse file tree 5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ All solutions will be accepted!
130
130
| 796| [ Rotate String] ( https://leetcode-cn.com/problems/rotate-string/description/ ) | [ java/py/js] ( ./algorithms/RotateString ) | Easy|
131
131
| 844| [ Backspace String Compare] ( https://leetcode-cn.com/problems/backspace-string-compare/description/ ) | [ java/py/js] ( ./algorithms/BackspaceStringCompare ) | Easy|
132
132
| 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|
133
134
| 112| [ Path Sum] ( https://leetcode-cn.com/problems/path-sum/description/ ) | [ java/py/js] ( ./algorithms/PathSum ) | Easy|
134
135
| 113| [ Path Sum II] ( https://leetcode-cn.com/problems/path-sum-ii/description/ ) | [ java/py/js] ( ./algorithms/PathSumII ) | Medium|
135
136
| 437| [ Path Sum III] ( https://leetcode-cn.com/problems/path-sum-iii/description/ ) | [ java/py/js] ( ./algorithms/PathSumIII ) | Easy|
Original file line number Diff line number Diff line change
1
+ # Remove Duplicates From Sorted Array II
2
+ We can solve this problem by double pointers
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments