Skip to content

Commit 06792ad

Browse files
committed
feat: update golang solution to lcci problems: No. 03.02, 03.04
1 parent 8ffe6ac commit 06792ad

File tree

6 files changed

+325
-8
lines changed

6 files changed

+325
-8
lines changed

lcci/03.02.Min Stack/README.md

+53-4
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ class MinStack {
6767
mins = new ArrayDeque<>();
6868
mins.push(Integer.MAX_VALUE);
6969
}
70-
70+
7171
public void push(int val) {
7272
s.push(val);
7373
mins.push(Math.min(mins.peek(), val));
7474
}
75-
75+
7676
public void pop() {
7777
s.pop();
7878
mins.pop();
7979
}
80-
80+
8181
public int top() {
8282
return s.peek();
8383
}
84-
84+
8585
public int getMin() {
8686
return mins.peek();
8787
}
@@ -137,6 +137,55 @@ class MinStack {
137137
*/
138138
```
139139

140+
### **Go**
141+
142+
```go
143+
type MinStack struct {
144+
stack []int
145+
minStack []int
146+
}
147+
148+
/** initialize your data structure here. */
149+
func Constructor() MinStack {
150+
return MinStack{
151+
stack: make([]int, 0),
152+
minStack: make([]int, 0),
153+
}
154+
}
155+
156+
func (this *MinStack) Push(x int) {
157+
this.stack = append(this.stack, x)
158+
if len(this.minStack) == 0 || x <= this.minStack[len(this.minStack)-1] {
159+
this.minStack = append(this.minStack, x)
160+
}
161+
}
162+
163+
func (this *MinStack) Pop() {
164+
v := this.stack[len(this.stack)-1]
165+
this.stack = this.stack[:len(this.stack)-1]
166+
if v == this.minStack[len(this.minStack)-1] {
167+
this.minStack = this.minStack[:len(this.minStack)-1]
168+
}
169+
}
170+
171+
func (this *MinStack) Top() int {
172+
return this.stack[len(this.stack)-1]
173+
}
174+
175+
func (this *MinStack) GetMin() int {
176+
return this.minStack[len(this.minStack)-1]
177+
}
178+
179+
/**
180+
* Your MinStack object will be instantiated and called as such:
181+
* obj := Constructor();
182+
* obj.Push(x);
183+
* obj.Pop();
184+
* param_3 := obj.Top();
185+
* param_4 := obj.GetMin();
186+
*/
187+
```
188+
140189
### **...**
141190

142191
```

lcci/03.02.Min Stack/README_EN.md

+53-4
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ class MinStack {
7878
mins = new ArrayDeque<>();
7979
mins.push(Integer.MAX_VALUE);
8080
}
81-
81+
8282
public void push(int val) {
8383
s.push(val);
8484
mins.push(Math.min(mins.peek(), val));
8585
}
86-
86+
8787
public void pop() {
8888
s.pop();
8989
mins.pop();
9090
}
91-
91+
9292
public int top() {
9393
return s.peek();
9494
}
95-
95+
9696
public int getMin() {
9797
return mins.peek();
9898
}
@@ -148,6 +148,55 @@ class MinStack {
148148
*/
149149
```
150150

151+
### **Go**
152+
153+
```go
154+
type MinStack struct {
155+
stack []int
156+
minStack []int
157+
}
158+
159+
/** initialize your data structure here. */
160+
func Constructor() MinStack {
161+
return MinStack{
162+
stack: make([]int, 0),
163+
minStack: make([]int, 0),
164+
}
165+
}
166+
167+
func (this *MinStack) Push(x int) {
168+
this.stack = append(this.stack, x)
169+
if len(this.minStack) == 0 || x <= this.minStack[len(this.minStack)-1] {
170+
this.minStack = append(this.minStack, x)
171+
}
172+
}
173+
174+
func (this *MinStack) Pop() {
175+
v := this.stack[len(this.stack)-1]
176+
this.stack = this.stack[:len(this.stack)-1]
177+
if v == this.minStack[len(this.minStack)-1] {
178+
this.minStack = this.minStack[:len(this.minStack)-1]
179+
}
180+
}
181+
182+
func (this *MinStack) Top() int {
183+
return this.stack[len(this.stack)-1]
184+
}
185+
186+
func (this *MinStack) GetMin() int {
187+
return this.minStack[len(this.minStack)-1]
188+
}
189+
190+
/**
191+
* Your MinStack object will be instantiated and called as such:
192+
* obj := Constructor();
193+
* obj.Push(x);
194+
* obj.Pop();
195+
* param_3 := obj.Top();
196+
* param_4 := obj.GetMin();
197+
*/
198+
```
199+
151200
### **...**
152201

153202
```

lcci/03.02.Min Stack/Solution.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
type MinStack struct {
2+
stack []int
3+
minStack []int
4+
}
5+
6+
/** initialize your data structure here. */
7+
func Constructor() MinStack {
8+
return MinStack{
9+
stack: make([]int, 0),
10+
minStack: make([]int, 0),
11+
}
12+
}
13+
14+
func (this *MinStack) Push(x int) {
15+
this.stack = append(this.stack, x)
16+
if len(this.minStack) == 0 || x <= this.minStack[len(this.minStack)-1] {
17+
this.minStack = append(this.minStack, x)
18+
}
19+
}
20+
21+
func (this *MinStack) Pop() {
22+
v := this.stack[len(this.stack)-1]
23+
this.stack = this.stack[:len(this.stack)-1]
24+
if v == this.minStack[len(this.minStack)-1] {
25+
this.minStack = this.minStack[:len(this.minStack)-1]
26+
}
27+
}
28+
29+
func (this *MinStack) Top() int {
30+
return this.stack[len(this.stack)-1]
31+
}
32+
33+
func (this *MinStack) GetMin() int {
34+
return this.minStack[len(this.minStack)-1]
35+
}
36+
37+
/**
38+
* Your MinStack object will be instantiated and called as such:
39+
* obj := Constructor();
40+
* obj.Push(x);
41+
* obj.Pop();
42+
* param_3 := obj.Top();
43+
* param_4 := obj.GetMin();
44+
*/

lcci/03.04.Implement Queue using Stacks/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,66 @@ class MyQueue {
130130
*/
131131
```
132132

133+
### **Go**
134+
135+
```go
136+
type MyQueue struct {
137+
s1, s2 []int
138+
}
139+
140+
/** Initialize your data structure here. */
141+
func Constructor() MyQueue {
142+
return MyQueue{
143+
s1: make([]int, 0),
144+
s2: make([]int, 0),
145+
}
146+
}
147+
148+
/** Push element x to the back of queue. */
149+
func (this *MyQueue) Push(x int) {
150+
this.s1 = append(this.s1, x)
151+
}
152+
153+
/** Removes the element from in front of queue and returns that element. */
154+
func (this *MyQueue) Pop() int {
155+
if len(this.s2) == 0 {
156+
this.transfer()
157+
}
158+
v := this.s2[len(this.s2)-1]
159+
this.s2 = this.s2[:len(this.s2)-1]
160+
return v
161+
}
162+
163+
/** Get the front element. */
164+
func (this *MyQueue) Peek() int {
165+
if len(this.s2) == 0 {
166+
this.transfer()
167+
}
168+
return this.s2[len(this.s2)-1]
169+
}
170+
171+
/** Returns whether the queue is empty. */
172+
func (this *MyQueue) Empty() bool {
173+
return len(this.s1) == 0 && len(this.s2) == 0
174+
}
175+
176+
func (this *MyQueue) transfer() {
177+
for len(this.s1) > 0 {
178+
this.s2 = append(this.s2, this.s1[len(this.s1)-1])
179+
this.s1 = this.s1[:len(this.s1)-1]
180+
}
181+
}
182+
183+
/**
184+
* Your MyQueue object will be instantiated and called as such:
185+
* obj := Constructor();
186+
* obj.Push(x);
187+
* param_2 := obj.Pop();
188+
* param_3 := obj.Peek();
189+
* param_4 := obj.Empty();
190+
*/
191+
```
192+
133193
### **...**
134194

135195
```

lcci/03.04.Implement Queue using Stacks/README_EN.md

+60
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,66 @@ class MyQueue {
151151
*/
152152
```
153153

154+
### **Go**
155+
156+
```go
157+
type MyQueue struct {
158+
s1, s2 []int
159+
}
160+
161+
/** Initialize your data structure here. */
162+
func Constructor() MyQueue {
163+
return MyQueue{
164+
s1: make([]int, 0),
165+
s2: make([]int, 0),
166+
}
167+
}
168+
169+
/** Push element x to the back of queue. */
170+
func (this *MyQueue) Push(x int) {
171+
this.s1 = append(this.s1, x)
172+
}
173+
174+
/** Removes the element from in front of queue and returns that element. */
175+
func (this *MyQueue) Pop() int {
176+
if len(this.s2) == 0 {
177+
this.transfer()
178+
}
179+
v := this.s2[len(this.s2)-1]
180+
this.s2 = this.s2[:len(this.s2)-1]
181+
return v
182+
}
183+
184+
/** Get the front element. */
185+
func (this *MyQueue) Peek() int {
186+
if len(this.s2) == 0 {
187+
this.transfer()
188+
}
189+
return this.s2[len(this.s2)-1]
190+
}
191+
192+
/** Returns whether the queue is empty. */
193+
func (this *MyQueue) Empty() bool {
194+
return len(this.s1) == 0 && len(this.s2) == 0
195+
}
196+
197+
func (this *MyQueue) transfer() {
198+
for len(this.s1) > 0 {
199+
this.s2 = append(this.s2, this.s1[len(this.s1)-1])
200+
this.s1 = this.s1[:len(this.s1)-1]
201+
}
202+
}
203+
204+
/**
205+
* Your MyQueue object will be instantiated and called as such:
206+
* obj := Constructor();
207+
* obj.Push(x);
208+
* param_2 := obj.Pop();
209+
* param_3 := obj.Peek();
210+
* param_4 := obj.Empty();
211+
*/
212+
```
213+
154214
### **...**
155215

156216
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
type MyQueue struct {
2+
s1, s2 []int
3+
}
4+
5+
/** Initialize your data structure here. */
6+
func Constructor() MyQueue {
7+
return MyQueue{
8+
s1: make([]int, 0),
9+
s2: make([]int, 0),
10+
}
11+
}
12+
13+
/** Push element x to the back of queue. */
14+
func (this *MyQueue) Push(x int) {
15+
this.s1 = append(this.s1, x)
16+
}
17+
18+
/** Removes the element from in front of queue and returns that element. */
19+
func (this *MyQueue) Pop() int {
20+
if len(this.s2) == 0 {
21+
this.transfer()
22+
}
23+
v := this.s2[len(this.s2)-1]
24+
this.s2 = this.s2[:len(this.s2)-1]
25+
return v
26+
}
27+
28+
/** Get the front element. */
29+
func (this *MyQueue) Peek() int {
30+
if len(this.s2) == 0 {
31+
this.transfer()
32+
}
33+
return this.s2[len(this.s2)-1]
34+
}
35+
36+
/** Returns whether the queue is empty. */
37+
func (this *MyQueue) Empty() bool {
38+
return len(this.s1) == 0 && len(this.s2) == 0
39+
}
40+
41+
func (this *MyQueue) transfer() {
42+
for len(this.s1) > 0 {
43+
this.s2 = append(this.s2, this.s1[len(this.s1)-1])
44+
this.s1 = this.s1[:len(this.s1)-1]
45+
}
46+
}
47+
48+
/**
49+
* Your MyQueue object will be instantiated and called as such:
50+
* obj := Constructor();
51+
* obj.Push(x);
52+
* param_2 := obj.Pop();
53+
* param_3 := obj.Peek();
54+
* param_4 := obj.Empty();
55+
*/

0 commit comments

Comments
 (0)