Skip to content

Commit 50ac15c

Browse files
committed
Add solution #80
1 parent ea8dcba commit 50ac15c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
74|[Search a 2D Matrix](./0074-search-a-2d-matrix.js)|Medium|
6262
77|[Combinations](./0077-combinations.js)|Medium|
6363
78|[Subsets](./0078-subsets.js)|Medium|
64+
80|[Remove Duplicates from Sorted Array II](./0080-remove-duplicates-from-sorted-array-ii.js)|Medium|
6465
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
6566
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
6667
90|[Subsets II](./0090-subsets-ii.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)