File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 73
73
| 77 | [ Combinations] ( https://leetcode.com/problems/combinations ) | [ ![ Java] ( assets/java.png )] ( src/Combinations.java ) | |
74
74
| 78 | [ Subsets] ( https://leetcode.com/problems/subsets ) | [ ![ Java] ( assets/java.png )] ( src/Subsets.java ) | |
75
75
| 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 ) | |
76
77
| 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 ) | |
77
78
| 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 ) | |
78
79
| 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 ) | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments