8000 update 100 solution · dycn/awesome-golang-leetcode@119b73f · GitHub
[go: up one dir, main page]

Skip to content

Commit 119b73f

Browse files
committed
update 100 solution
1 parent 155ef26 commit 119b73f

File tree

7 files changed

+231
-4
lines changed

7 files changed

+231
-4
lines changed

cmd/tree/tree.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gogf/gf/container/gtree"
6+
"github.com/gogf/gf/util/gutil"
7+
)
8+
9+
func main() {
10+
m := gtree.NewRedBlackTree(gutil.ComparatorInt)
11+
12+
// 设置键值对
13+
for i := 0; i < 10; i++ {
14+
m.Set(i, i*10)
15+
}
16+
// 查询大小
17+
fmt.Println(m.Size())
18+
// 批量设置键值对(不同的数据类型对象参数不同)
19+
m.Sets(map[interface{}]interface{}{
20+
10: 10,
21+
11: 11,
22+
})
23+
fmt.Println(m.Size())
24+
25+
// 查询是否存在
26+
fmt.Println(m.Contains(1))
27+
28+
// 查询键值
29+
fmt.Println(m.Get(1))
30+
31+
// 删除数据项
32+
m.Remove(9)
33+
fmt.Println(m.Size())
34+
35+
// 批量删除
36+
m.Removes([]interface{}{10, 11})
37+
fmt.Println(m.Size())
38+
39+
// 当前键名列表(随机排序)
40+
fmt.Println(m.Keys())
41+
// 当前键值列表(随机排序)
42+
fmt.Println(m.Values())
43+
44+
// 查询键名,当键值不存在时,写入给定的默认值
45+
fmt.Println(m.GetOrSet(100, 100))
46+
47+
// 删除键值对,并返回对应的键值
48+
fmt.Println(m.Remove(100))
49+
50+
// 遍历map
51+
m.IteratorAsc(func(k interface{}, v interface{}) bool {
52+
fmt.Printf("%v:%v ", k, v)
53+
return true
54+
})
55+
fmt.Println()
56+
m.Print()
57+
fmt.Println()
58+
59+
// 清空map
60+
m.Clear()
61+
62+
// 判断map是否为空
63+
fmt.Println(m.IsEmpty())
64+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module github.com/kylesliu/awesome-golang-leetcode
22

3-
require github.com/emirpasic/gods v1.12.0 // indirect
3+
require (
4+
github.com/emirpasic/gods v1.12.0 // indirect
5+
github.com/gogf/gf v1.9.10 // indirect
6+< 6D40 div class="diff-text-inner">)
47

58
go 1.13

go.sum

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
13
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
24
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
5+
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
6+
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
7+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
8+
github.com/gf-third/mysql v1.4.2/go.mod h1:+dd90V663ppI2fV5uQ6+rHk0u8KCyU6FkG8Um8Cx3ms=
9+
github.com/gf-third/yaml v1.0.1/go.mod h1:t443vj0txEw3+E0MOtkr83kt+PrZg2I8SRuYfn85NM0=
10+
github.com/gogf/gf v1.9.10 h1:lPBf0EOxv6AXVWN46EKLID0GMHDGOrs4ZAi/RUJbt+c=
11+
github.com/gogf/gf v1.9.10/go.mod h1:sitJZHjmT9B+923N4mH5rkM19AugKG+BxI47R64bR08=
12+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
13+
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
14+
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
15+
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
16+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
17+
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
18+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
19+
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
20+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
21+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
22+
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
23+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
24+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
25+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
26+
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27+
golang.org/x/sys v0.0.0-20190924092210-98129a5cf4a0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
30+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
31+
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
32+
google.golang.org/appengine v1.6.3/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=

src/0100.Same-Tree/Solution.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package Solution
22

3+
// 递归遍历
4+
// 时间复杂度 O(N)
5+
// 时间复杂度 O(tree height)
36
func isSameTree(p *TreeNode, q *TreeNode) bool {
47
if p == nil && q == nil {
58
return true
6-
}
7-
8-
if p == nil || q == nil {
9+
} else if p == nil || q == nil {
910
return false
1011
}
1112

@@ -16,3 +17,38 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
1617

1718
return false
1819
}
20+
21+
// 递归遍历
22+
// 时间复杂度 O(N)
23+
// 时间复杂度 O(tree height)
24+
25+
func isSameTree2(p *TreeNode, q *TreeNode) bool {
26+
qP := []*TreeNode{p}
27+
qQ := []*TreeNode{q}
28+
29+
for len(qP) != 0 && len(qQ) != 0 {
30+
pNode := qP[0]
31+
qP = qP[1:]
32+
33+
qNode := qQ[0]
34+
qQ = qQ[1:]
35+
36+
if pNode == nil && qNode == nil {
37+
continue
38+
}
39+
if pNode == nil && qNode != nil || pNode != nil && qNode == nil {
40+
return false
41+
}
42+
if pNode.Val != qNode.Val {
43+
return false
44+
}
45+
46+
qP = append(qP, pNode.Left, pNode.Right)
47+
qQ = append(qQ, qNode.Left, qNode.Right)
48+
}
49+
50+
if len(qP) == 0 && len(qQ) == 0 {
51+
return true
52+
}
53+
return false
54+
}

src/0278.First-Bad-Version/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [278. First Bad Version][title]
2+
3+
## Description
4+
5+
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
6+
7+
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
8+
9+
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
10+
**Example 1:**
11+
12+
```
13+
Given n = 5, and version = 4 is the first bad version.
14+
15+
call isBadVersion(3) -> false
16+
call isBadVersion(5) -> true
17+
call isBadVersion(4) -> true
18+
19+
Then 4 is the first bad version.
20+
```
21+
22+
**Tags:** Math, String
23+
24+
## 题意
25+
> 求2数之和
26+
27+
## 题解
28+
29+
### 思路1
30+
> 。。。。
31+
32+
```go
33+
34+
```
35+
36+
### 思路2
37+
> 思路2
38+
```go
39+
40+
```
41+
42+
## 结语
43+
44+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
45+
46+
[title]: https://leetcode.com/problems/two-sum/description/
47+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Solution
2+
3+
func Solution(x bool) bool {
4+
5+
return x
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs bool
14+
expect bool
15+
}{
16+
{"TestCase", true, true},
17+
{"TestCase", true, true},
18+
{"TestCase", false, false},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.inputs)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27+
c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
36+
}
37+
38+
// 使用案列
39+
func ExampleSolution() {
40+
41+
}

0 commit comments

Comments
 (0)
0