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 3fa279a

Browse files
authored
155 solved. (#66)
1 parent 8082f1c commit 3fa279a

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ continually updating 😃.
4141
* [717. 1-bit and 2-bit Characters](src/0717_1_bit_and_2_bit_characters/1bitand2bitc.go)
4242
* [747. Largest Number At Least Twice of Others](./src/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)
4343

44+
### Stack
45+
* [155. Min Stack](src/0155_min_stack/min_stack.go)
46+
4447
### String
4548
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*
4649
* [14. Longest Common Prefix](src/0014_longest_common_prefix/lcp.go)

‎src/0155_min_stack/min_stack.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
155. Min Stack
3+
https://leetcode.com/problems/min-stack/
4+
5+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6+
7+
push(x) -- Push element x onto stack.
8+
pop() -- Removes the element on top of the stack.
9+
top() -- Get the top element.
10+
getMin() -- Retrieve the minimum element in the stack.
11+
*/
12+
// time: 2019年01月10日
13+
14+
package minstack
15+
16+
// MinStack a stack that supports push, pop, top, and retrieving the minimum element in constant time.
17+
type MinStack struct {
18+
data []int
19+
}
20+
21+
// Constructor initialize your data structure here.
22+
func Constructor() MinStack {
23+
return MinStack{}
24+
}
25+
26+
// Push element x onto stack.
27+
func (s *MinStack) Push(x int) {
28+
s.data = append(s.data, x)
29+
}
30+
31+
// Pop Removes the element on top of the stack.
32+
func (s *MinStack) Pop() {
33+
if !s.IsEmpty() {
34+
s.data = s.data[:s.GetSize()-1]
35+
}
36+
}
37+
38+
// Top Get the top element.
39+
func (s *MinStack) Top() int {
40+
//if s.IsEmpty(){
41+
// return 0
42+
//}
43+
// stack must have element.
44+
return s.data[s.GetSize()-1]
45+
}
46+
47+
// GetMin retrieving the minimum element in constant time.
48+
// time complexity: O(n)
49+
func (s *MinStack) GetMin() int {
50+
//if s.IsEmpty() {
51+
// return 0
52+
//}
53+
// stack must have element.
54+
stackSize := s.GetSize()
55+
ret := s.data[stackSize-1]
56+
57+
for i := stackSize - 2; i >= 0; i-- {
58+
if s.data[i] < ret {
59+
ret = s.data[i]
60+
}
61+
}
62+
return ret
63+
}
64+
65+
// GetSize get size of the stack.
66+
func (s MinStack) GetSize() int {
67+
return len(s.data)
68+
}
69+
70+
func (s MinStack) IsEmpty() bool {
71+
return s.GetSize() == 0
72+
}

‎src/0155_min_stack/min_stack_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package minstack
2+
3+
import "testing"
4+
5+
func TestMinStack(t *testing.T) {
6+
obj := Constructor()
7+
obj.Push(-2)
8+
obj.Push(0)
9+
obj.Push(-3)
10+
if res := obj.GetMin(); res != -3 {
11+
t.Errorf("expected %d, got %d", -3, res)
12+
}
13+
14+
obj.Pop()
15+
if res := obj.Top(); res != 0 {
16+
t.Errorf("expected %d, got %d", 0, res)
17+
}
18+
if res := obj.GetMin(); res != -2 {
19+
t.Errorf("expected %d, got %d", -2, res)
20+
}
21+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
6161
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
6262
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
63+
|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|
6364
|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|
6465
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6566
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|

0 commit comments

Comments
(0)

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