Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

531-Week 01 #163

Merged
merged 1 commit into from Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions Week 01/id_531/LeetCode_189_531.go
@@ -0,0 +1,26 @@
package id531

func rotate(nums []int, k int) {
var pre int
for i:=0;i <k;i++ {
pre =nums[len(nums)-1]
for j:=0;j <len(nums);j++ {
pre,nums[j]=nums[j],pre
}
}
}

func rotate2(nums []int, k int) {
k%=len(nums)
reverse(nums,0,len(nums)-1)
reverse(nums,0,k-1)
reverse(nums,k,len(nums)-1)
}

func reverse(nums []int,start,end int) {
for start<end {
nums[start],nums[end] =nums[end],nums[start]
start++
end--
}
}
24 changes: 24 additions & 0 deletions Week 01/id_531/LeetCode_26_531.go
@@ -0,0 +1,24 @@
package id531

func removeDuplicates(nums []int) int {
b:=0
for a:=1;a<len(nums);a++ {
if nums[b]!=nums[a] {
nums[b+1]=nums[a]
b++
}
}
return b+1
}

func removeDuplicates2(nums []int) int {
count:=0
for i:=1;i<len(nums);i++ {
if nums[i]==nums[i-1] {
count++
}else {
nums[i-count]=nums[i]
}
}
return len(nums)-count
}
27 changes: 27 additions & 0 deletions Week 01/id_531/LeetCode_26_531_test.go
@@ -0,0 +1,27 @@
package id531

import "testing"

func Test_remove(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want int
}{
{
name:"removeDuplicates",
args:args{nums:[]int{1,1,2}},
want:2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeDuplicates(tt.args.nums); got != tt.want {
t.Errorf("remove() = %v, want %v", got, tt.want)
}
})
}
}
31 changes: 31 additions & 0 deletions Week 01/id_531/LeetCode_283_531.go
@@ -0,0 +1,31 @@
package id531

// MoveZeros1 按0 出现的次数挪动
func MoveZeros1(nums []int) {
zeroCount := 0
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
zeroCount++
} else {
if zeroCount > 0 {
nums[i-zeroCount] = nums[i]
nums[i] = 0
}
}
}
}

// MoveZeros2 双指针
func MoveZeros2(nums []int) {
j := 0
for i := 0; i < len(nums); i++ {
if nums[i] != 0 {
nums[j] = nums[i]
j++
}
}

for i := j; i < len(nums); i++ {
nums[i] = 0
}
}
67 changes: 67 additions & 0 deletions Week 01/id_531/LeetCode_283_531_test.go
@@ -0,0 +1,67 @@
package id531

import (
"reflect"
"testing"
)

func Test_moveZeros(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "move_zero_1",
args: args{
nums: []int{0, 1, 0, 3, 12},
},
want: []int{1, 3, 12, 0, 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
MoveZeros1(tt.args.nums)
if !reflect.DeepEqual(tt.args.nums, tt.want) {
t.Errorf("MoveZeros1() = %v, want %v", tt.args.nums, tt.want)
}
})
}
}

func TestMoveZeros2(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "move_zero_2",
args: args{
nums: []int{0, 1, 0, 3, 12},
},
want: []int{1, 3, 12, 0, 0},
},
{
name: "move_zero_2",
args: args{
nums: []int{0, 0, 0, 0, 12},
},
want: []int{12, 0, 0, 0, 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
MoveZeros2(tt.args.nums)
if !reflect.DeepEqual(tt.args.nums, tt.want) {
t.Errorf("MoveZeros2() = %v, want %v", tt.args.nums, tt.want)
}
})
}
}
25 changes: 25 additions & 0 deletions Week 01/id_531/leetCode_21_531.go
@@ -0,0 +1,25 @@

package id531



type ListNode struct {
Val int
Next *ListNode
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1==nil {
return l2
}
if l2 ==nil {
return l1
}
if l1.Val <l2.Val {
l1.Next=mergeTwoLists(l1.Next,l2)
return l1
}else {
l2.Next=mergeTwoLists(l1,l2.Next)
return l2
}
}