Skip to content

Commit b8215a2

Browse files
committed
Add solution and test-cases for problem 155
1 parent d50d9b2 commit b8215a2

File tree

2 files changed

+87
-11
lines changed

2 files changed

+87
-11
lines changed
Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const (
4+
push = "push"
5+
pop = "pop"
6+
top = "top"
7+
getMin = "getMIn"
8+
)
9+
10+
type MinStack struct {
11+
data, min []int
12+
}
13+
14+
func Constructor4() MinStack {
15+
return MinStack{
16+
data: make([]int, 0),
17+
min: make([]int, 0),
18+
}
19+
}
20+
21+
func (this *MinStack) Push(val int) {
22+
if len(this.data) == 0 {
23+
this.data = append(this.data, val)
24+
this.min = append(this.min, val)
25+
return
26+
}
27+
28+
top := this.min[len(this.min)-1]
29+
this.data = append(this.data, val)
30+
if top > val {
31+
top = val
32+
}
33+
34+
this.min = append(this.min, top)
35+
}
36+
37+
func (this *MinStack) Pop() {
38+
if len(this.data) > 0 {
39+
l := len(this.data)
40+
this.data = this.data[:l-1]
41+
this.min = this.min[:l-1]
42+
}
43+
}
44+
45+
func (this *MinStack) Top() int {
46+
if len(this.data) > 0 {
47+
return this.data[len(this.data)-1]
48+
}
49+
50+
return -1
51+
}
52+
53+
func (this *MinStack) GetMin() int {
54+
return this.min[len(this.min)-1]
55+
}
56+
57+
// action, push, pop, top, getMin
58+
func Solution(actions []string, vals []int) []int {
59+
o := Constructor4()
60+
expect := make([]int, 0)
61+
for idx, act := range actions {
62+
if act == push {
63+
o.Push(vals[idx])
64+
continue
65+
}
66+
if act == pop {
67+
o.Pop()
68+
continue
69+
}
70+
71+
if act == top {
72+
top := o.Top()
73+
expect = append(expect, top)
74+
continue
75+
}
76+
77+
m := o.GetMin()
78+
expect = append(expect, m)
79+
}
80+
return expect
581
}

leetcode/101-200/0155.Min-Stack/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
actions []string
14+
inputs []int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{push, push, push, getMin, pop, top, getMin}, []int{-2, 0, -3, 0, 0, 0, 0}, []int{-3, 0, -2}},
18+
{"TestCase2", []string{push, push, getMin, top, pop, push, getMin, top}, []int{1, 2, 0, 0, 0, -2, 0, 0}, []int{1, 2, -2, -2}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.actions, c.inputs)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with input-actions: %v, input-inputs: %v",
27+
c.expect, got, c.actions, c.inputs)
2828
}
2929
})
3030
}

0 commit comments

Comments
 (0)