Movatterモバイル変換


[0]ホーム

URL:


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

Commit8ceb9f4

Browse files
committed
remove_duplicates_from_sorted_array_26: solved
1 parentab45e53 commit8ceb9f4

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#26. Remove Duplicates from Sorted Array
2+
3+
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package remove_duplicates_from_sorted_array_26
2+
3+
funcremoveDuplicates(nums []int)int {
4+
i,j,u:=0,0,1
5+
forj<len(nums)-1 {
6+
j=i
7+
forj<len(nums)-1&&nums[i]==nums[j] {
8+
j++
9+
}
10+
ifnums[i]!=nums[j] {
11+
u++
12+
}
13+
14+
fork:=j-1;k>i;k-- {
15+
nums[k]=nums[j]
16+
}
17+
18+
i++
19+
}
20+
21+
returnu
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package remove_duplicates_from_sorted_array_26
2+
3+
import"testing"
4+
5+
funcTest_removeDuplicates(t*testing.T) {
6+
typeargsstruct {
7+
nums []int
8+
}
9+
tests:= []struct {
10+
namestring
11+
argsargs
12+
wantint
13+
}{
14+
{
15+
name:"remove duplicates",
16+
args:args{nums: []int{1,1,2}},
17+
want:2,
18+
},
19+
{
20+
name:"remove duplicates",
21+
args:args{nums: []int{1,1}},
22+
want:1,
23+
},
24+
{
25+
name:"remove duplicates",
26+
args:args{nums: []int{0,0,1,1,1,2,2,3,3,4}},
27+
want:5,
28+
},
29+
}
30+
for_,tt:=rangetests {
31+
t.Run(tt.name,func(t*testing.T) {
32+
ifgot:=removeDuplicates(tt.args.nums);got!=tt.want {
33+
t.Errorf("removeDuplicates() = %v, want %v",got,tt.want)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp