Skip to content

Commit

Permalink
leetcode: Add cpp impl to 0155
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 20, 2024
1 parent 43a5d86 commit 1bd53ea
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
- [0137. 只出现一次的数字II Single Number II](leetcode/0137.single-number-ii/index.md)
- [0153. 寻找旋转排序数组中的最小值 Find Minimum in Rotated Sorted Array](leetcode/0153.find-minimum-in-rotated-sorted-array/index.md)
- [0154. 寻找旋转排序数组中的最小值 II Find Minimum in Rotated Sorted Array II](leetcode/0154.find-minimum-in-rotated-sorted-array-ii/index.md)
- [0155. 最小栈 Min Stack](leetcode/0155.min-stack/index.md)
- [0162. 寻找峰值 Find Peak Element](leetcode/0162.find-peak-element/index.md)
- [0167. 两数之和 II - 输入有序数组 Two Sum II - Input Array Is Sorted](leetcode/0167.two-sum-ii-input-array-is-sorted/index.md)
- [0189. 旋转数组 Rotate Array](leetcode/0189.rotate-array/index.md)
Expand Down
2 changes: 2 additions & 0 deletions src/leetcode/0155.min-stack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dual-stack
/single-stack
6 changes: 6 additions & 0 deletions src/leetcode/0155.min-stack/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

dual-stack: dual-stack.cpp
g++ -g dual-stack.cpp -o dual-stack

single-stack: single-stack.cpp
g++ -g single-stack.cpp -o single-stack
44 changes: 44 additions & 0 deletions src/leetcode/0155.min-stack/dual-stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

#include <cassert>

#include <stack>

class MinStack {
public:
MinStack() {}

void push(int val) {
this->stack_.push(val);
if (this->min_stack_.empty()) {
this->min_stack_.push(val);
} else {
const int current_min = this->min_stack_.top();
const int new_min = std::min(current_min, val);
this->min_stack_.push(new_min);
}
}

void pop() {
this->stack_.pop();
this->min_stack_.pop();
}

int top() {
return this->stack_.top();
}

int getMin() {
return this->min_stack_.top();
}

private:
std::stack<int> stack_;
std::stack<int> min_stack_;
};

int main() {
return 0;
}
36 changes: 34 additions & 2 deletions src/leetcode/0155.min-stack/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
# 0155. 最小栈 Min Stack

#
[问题描述](https://leetcode.com/problems/min-stack)

[问题描述](../problems/)
这个问题可以有两种解法:

- 使用两个栈, 分别存放正常的数值, 和当前位置的最小值
- 使用一个栈, 但是栈中每个元素是一个元组, 分别存储 当前值和最小值

## 使用两个栈

### Rust

```rust
{{#include src/bin/min-stack-with-dual-vec.rs:5:63}}
```

### C++

```cpp
{{#include dual-stack.cpp:5:40}}
```
## 使用一个栈
### Rust
```rust
{{#include src/bin/min-stack-with-single-vec.rs:5:53}}
```

### C++

```cpp
{{#include single-stack.cpp:5:}}
```
42 changes: 42 additions & 0 deletions src/leetcode/0155.min-stack/single-stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

#include <cassert>

#include <stack>

class MinStack {
public:
MinStack() {}

void push(int val) {
if (this->stack_.empty()) {
this->stack_.emplace(val, val);
} else {
const int current_min = this->getMin();
const int new_min = std::min(current_min, val);
this->stack_.emplace(val, new_min);
}
}

void pop() {
this->stack_.pop();
}

int top() {
return this->stack_.top().first;
}

int getMin() {
return this->stack_.top().second;
}

private:
std::stack<std::pair<int, int>> stack_;
};

int main() {
return 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// in the LICENSE file.

// 用两个栈来实现

#[derive(Debug, Clone)]
pub struct MinStack {
// 正常的栈, 按入栈顺序存储.
Expand Down Expand Up @@ -31,8 +30,10 @@ impl MinStack {
}

pub fn push(&mut self, val: i32) {
// 将元素入栈
self.stack.push(val);
if let Some(top) = self.min_stack.last() {
// 保存当前位置最小的元素到 min_stack.
self.min_stack.push(*top.min(&val));
} else {
self.min_stack.push(val);
Expand Down
6 changes: 3 additions & 3 deletions src/leetcode/tags/stack/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

TODO:

- [155.最小栈](https://leetcode.com/problems/min-stack)
- [225.用队列实现栈](https://leetcode.com/problems/implement-stack-using-queues)
- [682.棒球比赛](https://leetcode.com/problems/baseball-game)
- [844.比较含退格的字符串](https://leetcode.com/problems/backspace-string-compare)
Expand All @@ -15,8 +14,9 @@ TODO:

## 中等

1. [0071.简化路径 Simplify Path](../../0071.simplify-path/index.md)
2. [0394. 字符串解码 Decode String](../../0394.decode-string/index.md)
1. [0071. 简化路径 Simplify Path](../../0071.simplify-path/index.md)
2. [0155. 最小栈 Min Stack](../../0155.min-stack/index.md)
3. [0394. 字符串解码 Decode String](../../0394.decode-string/index.md)

TODO:

Expand Down

0 comments on commit 1bd53ea

Please sign in to comment.