Skip to content

Commit 8574ed3

Browse files
committed
feat: add solution of Min Stack(155) with javascript.
1 parent 1d985d2 commit 8574ed3

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
| [125][125-question] | [Valid Palindrome][125-tips] | [Easy][E] | | [][125-js] | [][125-kotlin] |
8989
| [136][136-question] | [Single Number][136-tips] | [Easy][E] | | [][136-js] | [][136-kotlin] |
9090
| [141][141-question] | [Linked List Cycle][141-tips] | [Easy][E] | [][141-java] | | - |
91-
| [155][155-question] | [Min Stack][155-tips] | [Easy][E] | [][155-java] | | [][155-kotlin] |
91+
| [155][155-question] | [Min Stack][155-tips] | [Easy][E] | [][155-java] | [][155-js] | [][155-kotlin] |
9292
| [160][160-question] | [Intersection of Two Linked Lists][160-tips] | [Easy][E] | [][160-java] | | - |
9393
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
9494
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
@@ -467,6 +467,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
467467
[122-js]: ./src/_122/Solution.js
468468
[125-js]: ./src/_125/Solution.js
469469
[136-js]: ./src/_136/Solution.js
470+
[155-js]: ./src/_155/Solution.js
470471
[226-js]: ./src/_226/Solution.js
471472
[561-js]: ./src/_561/Solution.js
472473
[643-js]: ./src/_643/Solution.js

src/_155/Solution.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var MinStack = function() {
2+
this.stack = []
3+
};
4+
5+
/**
6+
* @param {number} x
7+
* @return {void}
8+
*/
9+
MinStack.prototype.push = function(x) {
10+
let min = this.stack.length === 0 ? x : this.stack[this.stack.length - 1].min
11+
this.stack.push({val: x, min: Math.min(min, x)})
12+
};
13+
14+
/**
15+
* @return {void}
16+
*/
17+
MinStack.prototype.pop = function() {
18+
if(this.stack.length > 0){
19+
this.stack.pop()
20+
}
21+
};
22+
23+
/**
24+
* @return {number}
25+
*/
26+
MinStack.prototype.top = function() {
27+
if(this.stack.length > 0) {
28+
return this.stack[this.stack.length - 1].val
29+
}
30+
};
31+
32+
/**
33+
* @return {number}
34+
*/
35+
MinStack.prototype.getMin = function() {
36+
if(this.stack.length > 0) {
37+
return this.stack[this.stack.length - 1].min
38+
}
39+
};

tips/155/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ class MinStack {
8585
}
8686
```
8787

88+
```javascript
89+
var MinStack = function() {
90+
this.stack = []
91+
};
92+
93+
/**
94+
* @param {number} x
95+
* @return {void}
96+
*/
97+
MinStack.prototype.push = function(x) {
98+
let min = this.stack.length === 0 ? x : this.stack[this.stack.length - 1].min
99+
this.stack.push({val: x, min: Math.min(min, x)})
100+
};
101+
102+
/**
103+
* @return {void}
104+
*/
105+
MinStack.prototype.pop = function() {
106+
if(this.stack.length > 0){
107+
this.stack.pop()
108+
}
109+
};
110+
111+
/**
112+
* @return {number}
113+
*/
114+
MinStack.prototype.top = function() {
115+
if(this.stack.length > 0) {
116+
return this.stack[this.stack.length - 1].val
117+
}
118+
};
119+
120+
/**
121+
* @return {number}
122+
*/
123+
MinStack.prototype.getMin = function() {
124+
if(this.stack.length > 0) {
125+
return this.stack[this.stack.length - 1].min
126+
}
127+
};
128+
129+
```
88130
## 结语
89131

90132
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

0 commit comments

Comments
 (0)