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

Commit f1217ad

Browse files
author
limingzhong
committed
add
1 parent 1a00059 commit f1217ad

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed

‎src/calculate/calculate.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package calculate
2+
3+
func calculate(s string) int {
4+
// 遍历s, 遇到数字追加到当前tempNum
5+
// sign 保存上一次的出现的符号,用于与当前数字进行组合,默认'+'
6+
// stack 用来保存所有数字与操作符的组合 +4 -3 *2 +1
7+
// 遇到 + - 将tempNum入栈,
8+
// 遇到 * / 将栈顶取出, 与当前数结合,再入栈
9+
// 将所有栈结果累加
10+
11+
sign := '+'
12+
tempNum := 0
13+
stack := make([]int, 0)
14+
15+
for i := 0; i < len(s); i++ {
16+
c := s[i]
17+
if c >= '0' && c <= '9' {
18+
tempNum = tempNum*10 + int(c-'0')
19+
} else {
20+
// 遇上操作符,需要将数字入栈,同时更新操作符缓存
21+
switch sign {
22+
case '+':
23+
stack = append(stack, tempNum)
24+
case '-':
25+
stack = append(stack, -tempNum)
26+
case '*':
27+
//top := stack[len(stack)-1]
28+
//stack[len(stack)-1] = top*tempNum
29+
stack[len(stack)-1] *= tempNum
30+
case '/':
31+
stack[len(stack)-1] /= tempNum
32+
}
33+
34+
// 更新sign tempNum
35+
sign = rune(c)
36+
tempNum = 0
37+
}
38+
}
39+
res := 0
40+
for i := 0; i < len(stack); i++ {
41+
res += stack[i]
42+
}
43+
return res
44+
}
45+
46+
func calculateV1(s string) int {
47+
48+
}
49+
50+
func calc(s string, i *int) int {
51+
// 计算不包含括号的表达式
52+
53+
res := 0
54+
sign := '+'
55+
tempNum := 0
56+
57+
for *i < len(s) {
58+
c := s[*i]
59+
if c >= '0' && c <= 9 {
60+
tempNum = tempNum*10 + int(c-'0')
61+
}
62+
63+
if c == '+' || c == '-' || *i == len(s)-1 {
64+
switch sign {
65+
case '+':
66+
res += tempNum
67+
case '-':
68+
res -= tempNum
69+
}
70+
sign = rune(c)
71+
tempNum = 0
72+
}
73+
74+
*i++
75+
if c == '(' {
76+
tempNum = calc(s, i)
77+
}
78+
if c == ')' {
79+
break
80+
}
81+
}
82+
return res
83+
}

‎src/interview/question.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package interview
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
func cacl(index string, a, b int) int {
10+
ret := a + b
11+
fmt.Println(index, a, b, ret)
12+
return ret
13+
}
14+
15+
func DeferAction() {
16+
a := 1
17+
b := 2
18+
19+
defer cacl("1", a, cacl("10", a, b))
20+
21+
b = 0
22+
defer cacl("2", a, cacl("20", a, b))
23+
b = 3
24+
25+
panic("exit")
26+
27+
// output
28+
// 10 1 2 3
29+
// 20 1 0 1
30+
// 2 1 1 2
31+
// 1 1 3 4
32+
// panic: exit
33+
}
34+
35+
func CloseChan() {
36+
type A struct {
37+
val int
38+
}
39+
40+
c := make(chan *A, 10)
41+
42+
c <- &A{val: 1}
43+
c <- &A{val: 2}
44+
close(c)
45+
46+
for i := range c {
47+
fmt.Println(i)
48+
}
49+
50+
fmt.Println(<-c)
51+
fmt.Println(<-c)
52+
53+
// output
54+
55+
//&{1}
56+
//&{2}
57+
//<nil>
58+
//<nil>
59+
}
60+
61+
func MapInit() {
62+
type A struct {
63+
val int
64+
}
65+
66+
// 以下用法错误 map的值是无法取址的,也就无法对结构体里的field进行操作; 因为map是无序的,并且随时存在扩容缩容,其地址也就不固定
67+
// m := map[string]A{"x": {1}}
68+
// m["x"].val = 2
69+
70+
// 解决方案
71+
// m := map[string]&A
72+
}
73+
74+
func ConText() {
75+
ctx, _ := context.WithCancel(context.Background())
76+
//defer cancel()
77+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
78+
79+
resp := make(chan int, 3)
80+
err := make(chan interface{}, 3)
81+
82+
operation := func(pid int, ctx context.Context, dst int, resp chan int, err chan interface{}) {
83+
n := 1
84+
for {
85+
select {
86+
case <-ctx.Done():
87+
fmt.Println(pid, "canceled", ctx.Err())
88+
err <- ctx.Err()
89+
return
90+
case <-time.After(time.Second):
91+
fmt.Println(pid, "1 second pass", n)
92+
n++
93+
if n == dst {
94+
resp <- pid
95+
return
96+
}
97+
}
98+
}
99+
}
100+
101+
go operation(1, ctx, 10, resp, err)
102+
go operation(2, ctx, 10, resp, err)
103+
go operation(3, ctx, 7, resp, err)
104+
105+
select {
106+
case pid := <-resp:
107+
fmt.Println(pid, "find dst and cancel other goroutines")
108+
case e := <-err:
109+
fmt.Println(e)
110+
}
111+
112+
cancel()
113+
time.Sleep(1 * time.Second) // wait for goroutines return
114+
}

‎src/interview/question_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package interview
2+
3+
import "testing"
4+
5+
func TestDeferAction(t *testing.T) {
6+
DeferAction()
7+
}
8+
9+
func TestCloseChan(t *testing.T) {
10+
CloseChan()
11+
}
12+
13+
func TestConText(t *testing.T) {
14+
ConText()
15+
}

‎src/sort/quick_sort.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sort
2+
3+
func Quicksort(nums []int, start, end int) {
4+
if start >= end {
5+
return
6+
}
7+
p := partition(nums, start, end)
8+
Quicksort(nums, start, p-1)
9+
Quicksort(nums, p+1, end)
10+
}
11+
12+
func partition(nums []int, start, end int) int {
13+
base := nums[end]
14+
i := start
15+
for j := start; j < end; j++ {
16+
if nums[j] < base {
17+
nums[i], nums[j] = nums[j], nums[i]
18+
i++
19+
}
20+
}
21+
nums[i], nums[end] = nums[end], nums[i]
22+
return i
23+
}
24+
25+
func Mergesort(nums []int) []int {
26+
if len(nums) <= 1 {
27+
return nums
28+
}
29+
30+
mid := len(nums) / 2
31+
left := Mergesort(nums[:mid])
32+
right := Mergesort(nums[mid:])
33+
34+
return merge(left, right)
35+
}
36+
37+
func merge(nums1, nums2 []int) []int {
38+
res := make([]int, 0)
39+
i, j := 0, 0
40+
for i < len(nums1) && j < len(nums2) {
41+
if nums1[i] <= nums2[j] {
42+
res = append(res, nums1[i])
43+
i++
44+
} else {
45+
res = append(res, nums2[j])
46+
j++
47+
}
48+
}
49+
50+
if i < len(nums1) {
51+
res = append(res, nums1[i:]...)
52+
}
53+
if j < len(nums2) {
54+
res = append(res, nums2[j:]...)
55+
}
56+
57+
return res
58+
}

‎src/sort/quick_sort_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sort
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestQuicksort(t *testing.T) {
9+
nums := []int{1, 3, 2, 5, 6, 4}
10+
Quicksort(nums, 0, len(nums)-1)
11+
fmt.Println(nums)
12+
}
13+
14+
func TestMergesort(t *testing.T) {
15+
nums := []int{1, 3, 2, 5, 6, 4}
16+
res := Mergesort(nums)
17+
fmt.Println(res)
18+
}

‎src/str/str.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Contain(haystack string, needle string) int {
2121
return i
2222
}
2323
}
24+
pause
2425
return -1
2526
}
2627

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /