Skip to content

Commit 26e35cf

Browse files
committed
feat: add solutions
1 parent 77b465a commit 26e35cf

File tree

63 files changed

+507
-80
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+507
-80
lines changed

.github/workflow/go-test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Go Unit Tests
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
jobs:
10+
test:
11+
name: Run Unit Tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: '1.21'
21+
22+
- name: Cache Go Modules
23+
uses: actions/cache@v3
24+
with:
25+
path: |
26+
~/go/pkg/mod
27+
./vendor
28+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Download dependencies
33+
run: go mod download
34+
35+
- name: Run Tests
36+
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
37+
38+
# - name: Upload coverage report
39+
# uses: codecov/codecov-action@v3
40+
# with:
41+
# files: coverage.out

data-structure/binary_tree/binary_tree.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,29 @@ func printBinaryTree(root *TreeNode, n int) {
5858
// 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的).
5959
fmt.Println(result[:n])
6060
}
61+
62+
func preOrder(root *TreeNode) {
63+
if root == nil {
64+
return
65+
}
66+
fmt.Println(root.Val)
67+
preOrder(root.Left)
68+
preOrder(root.Right)
69+
}
70+
func inOrder(root *TreeNode) {
71+
if root == nil {
72+
return
73+
}
74+
inOrder(root.Left)
75+
fmt.Println(root.Val)
76+
inOrder(root.Right)
77+
}
78+
79+
func postOrder(root *TreeNode) {
80+
if root == nil {
81+
return
82+
}
83+
postOrder(root.Left)
84+
postOrder(root.Right)
85+
fmt.Println(root.Val)
86+
}

data-structure/binary_tree/binary_tree_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ func TestBinaryTree(t *testing.T) {
77
root := constructBinaryTree(array)
88
printBinaryTree(root, len(array))
99
}
10+
11+
func TestDfs(t *testing.T) {
12+
array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8}
13+
root := constructBinaryTree(array)
14+
//preOrder(root)
15+
//inOrder(root)
16+
postOrder(root)
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sync"
7+
)
8+
9+
func producer(c chan int, val int, errChan chan error) {
10+
c <- val
11+
}
12+
13+
func consumer(c chan int, errChan chan error, wg *sync.WaitGroup) {
14+
select {
15+
case val, ok := <-c:
16+
if ok {
17+
fmt.Println(val)
18+
}
19+
case err := <-errChan:
20+
fmt.Printf("err: %v received, return", err)
21+
return
22+
}
23+
}
24+
25+
func main() {
26+
c := make(chan int, 100)
27+
errChan := make(chan error, 1)
28+
wg := &sync.WaitGroup{}
29+
for i := 0; i < 100; i++ {
30+
go producer(c, i, errChan)
31+
fmt.Println("channel size: ", len(c))
32+
wg.Add(1)
33+
go consumer(c, errChan, wg)
34+
}
35+
errChan <- fmt.Errorf("send finished")
36+
wg.Wait()
37+
runtime.NumGoroutine()
38+
}

golang/priority_channel/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# channel 优先级调度
2+
有a、b、c 3个channel,实现一个优先级调度,当a 先到达时优先使用a的,a没到达就使用b和c的,如何实现

golang/priority_channel/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// 使用 select 语句来轮询 a、b 和 c。
9+
// 在 select 中,如果 a 有数据,就优先选择 a,否则检查 b 和 c。
10+
// 使用 time.After 来避免阻塞,避免死锁的情况
11+
12+
func priorityChannel() {
13+
a := make(chan int)
14+
b := make(chan int)
15+
c := make(chan int)
16+
17+
go func() {
18+
time.Sleep(1 * time.Second)
19+
a <- 1 // 模拟a有数据,延迟1秒
20+
}()
21+
22+
go func() {
23+
time.Sleep(2 * time.Second)
24+
b <- 2 // 模拟b有数据,延迟2秒
25+
}()
26+
27+
go func() {
28+
time.Sleep(3 * time.Second)
29+
c <- 3 // 模拟c有数据,延迟3秒
30+
}()
31+
32+
for {
33+
select {
34+
case msgA := <-a:
35+
fmt.Println("Received from a:", msgA)
36+
case msgB := <-b:
37+
fmt.Println("Received from b:", msgB)
38+
case msgC := <-c:
39+
fmt.Println("Received from c:", msgC)
40+
}
41+
}
42+
}
43+
func priorityChannel2() {
44+
a := make(chan int)
45+
b := make(chan int)
46+
c := make(chan int)
47+
48+
go func() {
49+
time.Sleep(1 * time.Second)
50+
a <- 1 // 模拟a有数据,延迟1秒
51+
}()
52+
53+
go func() {
54+
time.Sleep(2 * time.Second)
55+
b <- 2 // 模拟b有数据,延迟2秒
56+
}()
57+
58+
go func() {
59+
time.Sleep(3 * time.Second)
60+
c <- 3 // 模拟c有数据,延迟3秒
61+
}()
62+
63+
for {
64+
select {
65+
case msgA := <-a:
66+
fmt.Println("Received from a:", msgA)
67+
case msgB := <-b:
68+
fmt.Println("Received from b:", msgB)
69+
case msgC := <-c:
70+
fmt.Println("Received from c:", msgC)
71+
}
72+
}
73+
}
74+
func main() {
75+
priorityChannel()
76+
priorityChannel2()
77+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package add_two_numbers
1+
package solutions
22

33
/**
44
* Definition for singly-linked list.
@@ -8,11 +8,6 @@ package add_two_numbers
88
* }
99
*/
1010

11-
type ListNode struct {
12-
Val int
13-
Next *ListNode
14-
}
15-
1611
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
1712
return nil
1813
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package best_time_to_buy_and_sell_stock
1+
package solutions
22

33
// https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-100-liked
44
// 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
@@ -27,7 +27,7 @@ func MaxProfit(prices []int) int {
2727
// 因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:如果我是在历史最低点买进的,那么我今天卖出能赚多少钱?
2828
// 当考虑完所有天数之时,我们就得到了最好的答案。
2929
func MaxProfit2(prices []int) int {
30-
minPrice := 99999999999999999999
30+
minPrice := 9999999
3131
maxProfit := 0
3232
for i := 0; i < len(prices); i++ {
3333
maxProfit = max(maxProfit, prices[i]-minPrice)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package best_time_to_buy_and_sell_stock
1+
package solutions
22

33
import (
44
"fmt"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package solutions
2+
3+
// https://leetcode.cn/problems/binary-tree-level-order-traversal/description/
4+
// 给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
5+
// 输入:root = [3,9,20,null,null,15,7]
6+
// 输出:[[3],[9,20],[15,7]]
7+
// 示例 2:
8+
//
9+
// 输入:root = [1]
10+
// 输出:[[1]]
11+
// 示例 3:
12+
//
13+
// 输入:root = []
14+
// 输出:[]
15+
//
16+
// 提示:
17+
//
18+
// 树中节点数目在范围 [0, 2000] 内
19+
// -1000 <= Node.val <= 1000
20+
func levelOrder(root *TreeNode) [][]int {
21+
if root == nil {
22+
return nil
23+
}
24+
var ans [][]int
25+
var currentNodes []*TreeNode
26+
currentNodes = append(currentNodes, root) // 根节点开始遍历每一层
27+
for currentNodes != nil {
28+
var currAns []int
29+
var nextNodes []*TreeNode
30+
for _, node := range currentNodes {
31+
currAns = append(currAns, node.Val) //把这一层的数据加进去
32+
if node.Left != nil {
33+
nextNodes = append(nextNodes, node.Left) //把下一层要遍历的节点加进去
34+
}
35+
if node.Right != nil {
36+
nextNodes = append(nextNodes, node.Right)
37+
}
38+
}
39+
ans = append(ans, currAns)
40+
currentNodes = nextNodes
41+
}
42+
return ans
43+
}

0 commit comments

Comments
 (0)