Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98095f7

Browse files
solves remove duplicates fromsorted array ii
1 parent 27f3ba2 commit 98095f7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
| 77 | [Combinations](https://leetcode.com/problems/combinations) | [![Java](assets/java.png)](src/Combinations.java) | |
7474
| 78 | [Subsets](https://leetcode.com/problems/subsets) | [![Java](assets/java.png)](src/Subsets.java) | |
7575
| 79 | [Word Search](https://leetcode.com/problems/word-search) | [![Java](assets/java.png)](src/WordSearch.java) | |
76+
| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedArrayII.java) | |
7677
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
7778
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
7879
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class RemoveDuplicatesFromSortedArrayII {
6+
public int removeDuplicates(int[] nums) {
7+
int insertionIndex = 1;
8+
for (int i = 1, count = 1 ; i < nums.length ; i++) {
9+
if (nums[i] == nums[insertionIndex - 1]) {
10+
if (count == 2) continue;
11+
nums[insertionIndex++] = nums[i];
12+
count++;
13+
} else {
14+
nums[insertionIndex++] = nums[i];
15+
count = 1;
16+
}
17+
}
18+
return insertionIndex;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /