Skip to content

Commit 1c44b3f

Browse files
skrbugskrbug
skrbug
authored and
skrbug
committed
first commit
1 parent 350f6df commit 1c44b3f

File tree

31 files changed

+1387
-38
lines changed

31 files changed

+1387
-38
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package leetcode11
2+
3+
// https://leetcode-cn.com/problems/container-with-most-water/
4+
5+
// area distance * minHeight
6+
// best method
7+
func maxArea(height []int) int {
8+
9+
if len(height) < 2 {
10+
return 0
11+
}
12+
13+
result,left,right := 0,0,len(height) - 1
14+
result = (right - left) * min(height[left],height[right])
15+
for left < right {
16+
if height[left] < height[right]{
17+
left ++
18+
}else {
19+
right --
20+
21+
}
22+
thisResult := (right - left) * min(height[left],height[right])
23+
result = max(result,thisResult)
24+
}
25+
return result
26+
}
27+
28+
func min(x,y int) int{
29+
if x < y {
30+
return x
31+
}
32+
return y
33+
}
34+
35+
func max(x,y int) int{
36+
if x > y {
37+
return x
38+
}
39+
return y
40+
}
41+
42+
43+
44+
45+
// force
46+
func maxAreaForce(height []int) int {
47+
n := len(height)
48+
if n < 2 {
49+
return 0
50+
}
51+
52+
result := 0
53+
for i := 0; i < n ; i ++ {
54+
for j := 0; j <n;j ++ {
55+
if height[j] < height[i] || j == i {
56+
continue
57+
}
58+
dV := distance(j , i)
59+
result = max(result,dV * height[i])
60+
}
61+
}
62+
return result
63+
}
64+
65+
func distance(x,y int)int {
66+
if x < y {
67+
return y - x
68+
}else{
69+
return x - y
70+
}
71+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode11
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMaxArea(t *testing.T){
10+
tests := []struct{
11+
name string
12+
arg []int
13+
want int
14+
}{
15+
{
16+
"Double Pointer",
17+
[]int{1,8,6,2,5,4,8,3,7},
18+
49,
19+
},
20+
{
21+
"Force soulution",
22+
[]int{1,8,6,2,5,4,8,3,7},
23+
49,
24+
},
25+
}
26+
27+
for _,tt:=range tests{
28+
t.Run(tt.name,func(t *testing.T){
29+
assert.Equal(t,tt.want,maxArea(tt.arg))
30+
})
31+
}
32+
33+
}

141_linked-list-cycle/leetcode141.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
type ListNode struct{
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
// 快慢指针法,让其两个指针速度不一样,如果有环一定会相撞
9+
func hasCycle(head *ListNode) bool {
10+
fast,slow := head,head
11+
if head == nil{
12+
return false
13+
}
14+
for {
15+
if fast.Next ==nil || fast.Next.Next == nil {
16+
return false
17+
}
18+
19+
fast = fast.Next.Next
20+
slow = slow.Next
21+
22+
if fast == slow {
23+
return true
24+
}
25+
26+
}
27+
28+
}

142_linked-list-cycle/leetcode141.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
type ListNode struct{
4+
Val int
5+
Next *ListNode
6+
}
7+
/*
8+
9+
10+
11+
判断环原理:
12+
13+
定义
14+
x1 令牌环head到环的第一个节点的步数,即我们要求的那个节点
15+
x2 从环的第一个点到相遇的那个点,需要x2步
16+
x3 从环中相遇点回到环的第一个点需要x3步
17+
18+
19+
fast、slow会相遇,说明走的时间是一定的
20+
路程有如下关系
21+
fast : t = (x1 + x2 + x3 + x2) / 2
22+
slow: t = (x1 + x2) / 1
23+
24+
得到 x1 = x3
25+
26+
相遇后,我们让快指针指向头节点,然后步频为1的走,下次相遇即是x1的位置
27+
28+
29+
*/
30+
31+
32+
func detectCycle(head *ListNode) *ListNode {
33+
fast,slow := head,head
34+
if head == nil{
35+
return nil
36+
}
37+
for {
38+
if fast.Next ==nil || fast.Next.Next == nil {
39+
return nil
40+
}
41+
42+
fast = fast.Next.Next
43+
slow = slow.Next
44+
if fast == slow {
45+
break
46+
}
47+
}
48+
fast = head
49+
for fast != slow {
50+
fast = fast.Next
51+
slow = slow.Next
52+
}
53+
return fast
54+
55+
}

15_3sum/leetcode15.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package leetcode15
2+
3+
import "sort"
4+
5+
// https://leetcode-cn.com/problems/3sum/
6+
7+
// a + b + c = 0
8+
// 去重问题难解决,如果用java,可以用Set容器装,go语言需要自己处理去重
9+
// func threeSum(nums []int) [][]int {
10+
// sort.Slice(nums,func(i, j int) bool {
11+
// if nums[i] <= nums[j] {
12+
// return true
13+
// }
14+
// return false
15+
// })
16+
// result := make([][]int,0)
17+
18+
// for i:=0 ; i < len(nums) - 2 ; i ++ {
19+
// a := nums[i]
20+
21+
// for j,k := i + 1, len(nums) - 1; j < k;{
22+
// b := nums[j]
23+
// c := nums[k]
24+
// if a + b + c == 0 {
25+
// combol := []int{a,b,c}
26+
// result = append(result, combol)
27+
// break
28+
// }else if a + b + c < 0 {
29+
// j ++
30+
// }else {
31+
// k --
32+
// }
33+
34+
// }
35+
// }
36+
// return result
37+
// }
38+
39+
func threeSum(nums []int)[][]int{
40+
res := [][]int{}
41+
42+
counter := map[int]int{}
43+
44+
for _,value := range nums {
45+
counter[value] ++
46+
}
47+
uniques := []int{}
48+
for key := range counter {
49+
uniques = append(uniques,key)
50+
}
51+
52+
sort.Ints(uniques)
53+
54+
for i := 0; i < len(uniques); i++ {
55+
if uniques[i] * 3 == 0 && counter[uniques[i]] >= 3 {
56+
res = append(res, []int{uniques[i],uniques[i],uniques[i]})
57+
}
58+
59+
for j := i + 1; j < len(uniques); j++ {
60+
if uniques[i] * 2 + uniques[j] == 0 && counter[uniques[i]] > 1 {
61+
res = append(res, []int{uniques[i],uniques[i],uniques[j]})
62+
}
63+
64+
if uniques[j] * 2 + uniques[i] == 0 && counter[uniques[j]] > 1 {
65+
res = append(res, []int{uniques[i],uniques[j],uniques[j]})
66+
}
67+
68+
c := 0 - uniques[i] - uniques[j]
69+
if c > uniques[j] && counter[c] > 0 {
70+
res = append(res, []int{uniques[i],uniques[j],c})
71+
}
72+
73+
}
74+
75+
}
76+
77+
return res
78+
}
79+
80+
81+

15_3sum/leetcode15_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode15
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test3Sum(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
arg []int
13+
want [][]int
14+
}{
15+
{ "3sum",
16+
[]int{-1, 0, 1, 2, -1, -4},
17+
[][]int{
18+
[]int{-1, 0, 1},
19+
[]int{-1, -1, 2},
20+
},
21+
},
22+
}
23+
24+
for _,tt := range tests {
25+
t.Run(tt.name,func(t *testing.T) {
26+
assert.Equal(t,tt.want,threeSum(tt.arg))
27+
})
28+
}
29+
30+
}

189_rotate_array/leetcode.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
4+
5+
// 旋转数组
6+
func rotate(nums []int,k int){
7+
for i := 0; i < k; i++ {
8+
last := nums[len(nums) - 1]
9+
for j := len(nums) - 1; j- 1 >= 0; j-- {
10+
nums[j] = nums[j-1]
11+
}
12+
nums[0] = last
13+
}
14+
}
15+
16+
17+
// 最优解,每个元素只移动了一次,每个元素都是一步到位
18+
func rotate2(nums []int,k int){
19+
k = k % len(nums)
20+
for start,count := 0,0; count < len(nums);start ++ {
21+
current := start
22+
pre := nums[start]
23+
for {
24+
next := (current + k) % len(nums)
25+
tmp := nums[next]
26+
nums[next] = pre
27+
pre = tmp
28+
current = next
29+
count ++
30+
if start == current { // java 当中的do-while语句,在这里只能 for 死循环加上最后判断,才能实现至少执行一次的效果
31+
break
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)