Skip to content

Commit ba1ceee

Browse files
committed
remove duplicate from sorted array solved
1 parent d6bd5d8 commit ba1ceee

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ continually updating 😃.
1111

1212
### Array
1313
* [1. Two Sum](./src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
14+
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
1415
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
1516
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
1617
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
26. Remove Duplicates from Sorted Array
3+
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
4+
5+
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
6+
7+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
8+
*/
9+
// time: 2018-12-20
10+
11+
package rdfsa
12+
13+
// double index
14+
// time complexity: O(n)
15+
// space complexity: O(1)
16+
func removeDuplicates(nums []int) int {
17+
n := len(nums)
18+
19+
if 0 == n {
20+
return n
21+
}
22+
23+
var (
24+
res = 1
25+
i = 1
26+
index = nextDifferentCharacterIndex(nums, 1) // 下一个不重复的地址
27+
)
28+
29+
for index < n {
30+
res++
31+
nums[i] = nums[index]
32+
i++
33+
index = nextDifferentCharacterIndex(nums, index+1)
34+
}
35+
return res
36+
}
37+
38+
func nextDifferentCharacterIndex(nums []int, p int) int {
39+
for ; p < len(nums); p++ {
40+
if nums[p] != nums[p-1] {
41+
break
42+
}
43+
}
44+
return p
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package rdfsa
2+
3+
import "testing"
4+
5+
func TestRemoveDuplicates(t *testing.T) {
6+
// removeDuplicates([]int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4})
7+
testCases := [][]int{
8+
{1, 1, 2},
9+
{0, 0, 1, 1, 1, 2, 2, 3, 3, 4},
10+
}
11+
12+
expected := []int{2, 5}
13+
14+
for index, data := range testCases {
15+
if res := removeDuplicates(data); expected[index] != res {
16+
t.Errorf("expected %d, got %d", expected[index], res)
17+
}
18+
}
19+
}

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
1010
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
1111
|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|
12+
|0026|[Remove Duplicates from Sorted Array](0026_remove_duplicates_from_sorted_array/rdfsa.go)|Easy|*`array;`* *`double index`*|
1213
|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|
1314
|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|
1415
|0034|[ Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|

0 commit comments

Comments
 (0)