-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26-RemoveDuplicatesfromSortedArray.go
More file actions
132 lines (115 loc) · 4.71 KB
/
26-RemoveDuplicatesfromSortedArray.go
File metadata and controls
132 lines (115 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
// 26. Remove Duplicates from Sorted Array
// Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.
// The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
// Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
// Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
// Return k.
// Custom Judge:
// The judge will test your solution with the following code:
// int[] nums = [...]; // Input array
// int[] expectedNums = [...]; // The expected answer with correct length
// int k = removeDuplicates(nums); // Calls your implementation
// assert k == expectedNums.length;
// for (int i = 0; i < k; i++) {
// assert nums[i] == expectedNums[i];
// }
// If all assertions pass, then your solution will be accepted.
// Example 1:
// Input: nums = [1,1,2]
// Output: 2, nums = [1,2,_]
// Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
// It does not matter what you leave beyond the returned k (hence they are underscores).
// Example 2:
// Input: nums = [0,0,1,1,1,2,2,3,3,4]
// Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
// Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
// It does not matter what you leave beyond the returned k (hence they are underscores).
// Constraints:
// 1 <= nums.length <= 3 * 10^4
// -100 <= nums[i] <= 100
// nums is sorted in non-decreasing order.
import "fmt"
//allocate extra space for another array
func removeDuplicates1(nums []int) int {
if len(nums) < 2 {
return len(nums)
}
res, arr := 1, []int{nums[0]} // 声明一个数组来保存 去重后的数据
for i := 1; i < len(nums); i++ {
if nums[i] != arr[res - 1] { //
arr = append(arr, nums[i])
res++
}
}
return res
}
func removeDuplicates(nums []int) int {
l, res, t := len(nums), 1, nums[0]
if l < 2 {
return l
}
for i := 1; i < l; i++ {
if nums[i] != t {
t = nums[i]
res++
}
nums[res - 1] = nums[i] // it is the point
}
return res
}
func removeDuplicates2(nums []int) int {
if len(nums) == 0 {
return 0
}
last, finder := 0, 0
for last < len(nums)-1 {
for nums[finder] == nums[last] {
finder++
if finder == len(nums) {
return last + 1
}
}
nums[last+1] = nums[finder]
last++
}
return last + 1
}
// best solution
func removeDuplicates3(nums []int) int {
j := 0
for i := 1; i < len(nums); i++ { // 注意从 1 开始
if nums[j] != nums[i] {
j++
nums[j] = nums[i] // 用已使用的地址来保存去重的数据
// nums: 1 1 2 3 3
// r1 nums: 1 1 2 3 3 j:0 i:1 因为 nums[0] == nums[1] 所以不进入
// r2 nums: 1 2 2 3 3 j:1 i:2 因为 nums[0] !+ nums[2] 所有 j++ (1) nums[1] = nums[2
// r3 nums: 1 2 3 3 3 j:2 i:3
// r3 nums: 1 2 3 3 3 j:2 i:4
}
}
return j + 1
}
func main() {
// Input: nums = [1,1,2]
// Output: 2, nums = [1,2,_]
// Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
// It does not matter what you leave beyond the returned k (hence they are underscores).
a1 := []int{1,1,2}
fmt.Println("before: ", a1)
fmt.Println(removeDuplicates3(a1)) // 2 [1,2,_]
fmt.Println("after: ", a1)
a2 := []int{0,0,1,1,1,2,2,3,3,4}
fmt.Println("before: ", a2)
fmt.Println(removeDuplicates3(a2)) // 5 [0,1,2,3,4,_,_,_,_,_]
fmt.Println("after: ", a2)
// Example 2:
// Input: nums = [0,0,1,1,1,2,2,3,3,4]
// Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
fmt.Printf("removeDuplicates([]int{1,1,5,6,7,8,9,9,10,11,23}) = %v\n",removeDuplicates([]int{1,1,5,6,7,8,9,9,10,11,23}))
fmt.Printf("removeDuplicates1([]int{1,1,5,6,7,8,9,9,10,11,23}) = %v\n",removeDuplicates1([]int{1,1,5,6,7,8,9,9,10,11,23}))
fmt.Printf("removeDuplicates2([]int{1,1,5,6,7,8,9,9,10,11,23}) = %v\n",removeDuplicates2([]int{1,1,5,6,7,8,9,9,10,11,23}))
fmt.Printf("removeDuplicates3([]int{1,1,5,6,7,8,9,9,10,11,23}) = %v\n",removeDuplicates3([]int{1,1,5,6,7,8,9,9,10,11,23}))
fmt.Printf("removeDuplicates3([]int{1,1,2,3,3}) = %v\n",removeDuplicates3([]int{1,1,2,3,3}))
}