Skip to content

Commit 698fcb3

Browse files
authored
80 remove duplicates 2 solved (#11)
1 parent 3b85ebc commit 698fcb3

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ continually updating 😃.
1313
* [1. Two Sum](./src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
1414
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
1515
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
16+
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
1617
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
1718
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
1819
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
80. Remove Duplicates from Sorted Array II
3+
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
4+
5+
Given a sorted array nums, remove the duplicates in-place such that
6+
duplicates appeared at most twice and return the new length.
7+
8+
Do not allocate extra space for another array, you must do this
9+
by modifying the input array in-place with O(1) extra memory.
10+
*/
11+
// time: 2018-12-21
12+
13+
package rdfsa2
14+
15+
// double index
16+
// time complexity: O(n)
17+
// space complexity: O(1)
18+
func removeDuplicates(nums []int) int {
19+
n := len(nums)
20+
if 0 == n || 1 == n {
21+
return n
22+
}
23+
24+
var (
25+
res = 2
26+
i = 2
27+
index = nextDifferentCharacterIndex(nums, i, 2) // 寻找下一个满足条件的下标
28+
)
29+
30+
for index < n {
31+
res++
32+
nums[i] = nums[index]
33+
i++
34+
index = nextDifferentCharacterIndex(nums, i, index+1)
35+
}
36+
return res
37+
}
38+
39+
func nextDifferentCharacterIndex(nums []int, j int, p int) int {
40+
for ; p < len(nums); p++ {
41+
if nums[j-1] == nums[j-2] && nums[p] != nums[j-1] || nums[j-1] != nums[j-2] {
42+
break
43+
}
44+
}
45+
return p
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package rdfsa2
2+
3+
import "testing"
4+
5+
func TestRemoveDuplicates(t *testing.T) {
6+
testCases := [][]int{
7+
{1, 1, 1, 2, 2, 3},
8+
{1, 2, 2},
9+
{0, 0, 1, 1, 1, 1, 2, 3, 3},
10+
{},
11+
{1},
12+
{2, 2},
13+
{2, 2, 2},
14+
}
15+
expected := []int{5, 3, 7, 0, 1, 2, 2}
16+
17+
for index, data := range testCases {
18+
if res := removeDuplicates(data); res != expected[index] {
19+
t.Errorf("expected %d, got %d", expected[index], res)
20+
}
21+
}
22+
}

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
|0069|[Sqrt(x)](0069_sqrtx/sqrtx.go)|Easy|*`binary search`*|
2222
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
2323
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
24+
|0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*|
2425
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|
2526
|0100|[Same Tree](./0100_same_tree/same_tree.go)|Easy|*`binary tree`*|
2627
|0101|[Symmetric Tree](./0101_symmetric_tree/symmetric_tree.go)|Easy|*`stack;`* *`recursion; `* *`iterative`*|

0 commit comments

Comments
 (0)