File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 61
61
74|[ Search a 2D Matrix] ( ./0074-search-a-2d-matrix.js ) |Medium|
62
62
77|[ Combinations] ( ./0077-combinations.js ) |Medium|
63
63
78|[ Subsets] ( ./0078-subsets.js ) |Medium|
64
+ 80|[ Remove Duplicates from Sorted Array II] ( ./0080-remove-duplicates-from-sorted-array-ii.js ) |Medium|
64
65
83|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
65
66
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
66
67
90|[ Subsets II] ( ./0090-subsets-ii.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 80. Remove Duplicates from Sorted Array II
3
+ * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such
7
+ * that each unique element appears at most twice. The relative order of the elements should be kept
8
+ * the same.
9
+ *
10
+ * Since it is impossible to change the length of the array in some languages, you must instead have
11
+ * the result be placed in the first part of the array nums. More formally, if there are k elements
12
+ * after removing the duplicates, then the first k elements of nums should hold the final result.
13
+ * It does not matter what you leave beyond the first k elements.
14
+ *
15
+ * Return k after placing the final result in the first k slots of nums.
16
+ *
17
+ * Do not allocate extra space for another array. You must do this by modifying the input array
18
+ * in-place with O(1) extra memory.
19
+ */
20
+
21
+ /**
22
+ * @param {number[] } nums
23
+ * @return {number }
24
+ */
25
+ var removeDuplicates = function ( nums ) {
26
+ let offset = 2 ;
27
+
28
+ for ( let i = 2 ; i < nums . length ; i ++ ) {
29
+ if ( nums [ i ] !== nums [ offset - 2 ] ) {
30
+ nums [ offset ] = nums [ i ] ;
31
+ offset ++ ;
32
+ }
33
+ }
34
+
35
+ return offset ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments