Skip to content

Update 155.min-stack.md #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 219 additions & 3 deletions problems/155.min-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pop或者top的时候:

## 代码

* 语言支持:JS,Python
* 语言支持:JS,C++,Java,Python

Javascript Code:

Expand Down Expand Up @@ -139,6 +139,123 @@ MinStack.prototype.min = function() {
*/
```

C++ Code:

```c++
class MinStack {
stack<long> data;
long min = INT_MAX;
public:
/** initialize your data structure here. */
MinStack() {

}

void push(int x) {
data.push(x - min);
if(x < min)
{
min = x;
}

}

void pop() {
long top = data.top();
data.pop();
// 更新最小值
if(top < 0)
{
min -= top;
}

}

int top() {
long top = data.top();
// 最小值为 min
if (top < 0)
{
return min;
}
else{
return min+top;
}
}

int getMin() {
return min;
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
```


Java Code:

```java
class MinStack {
long min;
Stack<Long> stack;

/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}

public void push(int x) {
if (stack.isEmpty()) {
stack.push(0L);
min = x;
}
else {
stack.push(x - min);
if (x < min)
min = x;
}
}

public void pop() {
long p = stack.pop();

if (p < 0) {
// if (p < 0), the popped value is the min
// Recall p is added by this statement: stack.push(x - min);
// So, p = x - old_min
// old_min = x - p
// again, if (p < 0), x is the min so:
// old_min = min - p
min = min - p;
}
}

public int top() {
long p = stack.peek();

if (p < 0) {
return (int) min;
}
else {
// p = x - min
// x = p + min
return (int) (p + min);
}
}

public int getMin() {
return (int) min;
}
}
```

Python Code:

```python
Expand Down Expand Up @@ -207,7 +324,7 @@ class MinStack:

## 代码

JavaScript
JavaScript Code:

```js
/**
Expand Down Expand Up @@ -263,8 +380,107 @@ MinStack.prototype.min = function() {
*/
```

C++ Code:
```c++
class MinStack {
stack<int> data;
stack<int> helper;
public:
/** initialize your data structure here. */
MinStack() {

}

void push(int x) {
data.push(x);
if(helper.empty() || helper.top() >= x)
{
helper.push(x);
}

}

void pop() {
int top = data.top();
data.pop();
if(top == helper.top())
{
helper.pop();
}

}

int top() {
return data.top();
}

int getMin() {
return helper.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
```

Java Code:
```java
public class MinStack {

// 数据栈
private Stack<Integer> data;
// 辅助栈
private Stack<Integer> helper;

/**
* initialize your data structure here.
*/
public MinStack() {
data = new Stack<>();
helper = new Stack<>();
}

public void push(int x) {
// 辅助栈在必要的时候才增加
data.add(x);
if (helper.isEmpty() || helper.peek() >= x) {
helper.add(x);
}
}

public void pop() {
// 关键 3:data 一定得 pop()
if (!data.isEmpty()) {
// 注意:声明成 int 类型,这里完成了自动拆箱,从 Integer 转成了 int,
// 因此下面的比较可以使用 "==" 运算符
int top = data.pop();
if(top == helper.peek()){
helper.pop();
}
}
}

public int top() {
if(!data.isEmpty()){
return data.peek();
}
}

public int getMin() {
if(!helper.isEmpty()){
return helper.peek();
}
}
}
```

Python3:
Python3 Code:

```python
class MinStack:
Expand Down