Skip to content

Commit

Permalink
leetcode: Add cpp impl to 0150
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 20, 2024
1 parent 1bd53ea commit c81722f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
- [0125. 验证回文串 Valid Palindrome](leetcode/0125.valid-palindrome/index.md)
- [0136. 只出现一次的数字 Single Number](leetcode/0136.single-number/index.md)
- [0137. 只出现一次的数字II Single Number II](leetcode/0137.single-number-ii/index.md)
- [0150. 逆波兰表达式求值 Evaluate Reverse Polish Notation](leetcode/0150.evaluate-reverse-polish-notation/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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/main
3 changes: 3 additions & 0 deletions src/leetcode/0150.evaluate-reverse-polish-notation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

build: main.cpp
g++ -g main.cpp -o main
21 changes: 19 additions & 2 deletions src/leetcode/0150.evaluate-reverse-polish-notation/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# 0150. 逆波兰表达式求值 Evaluate Reverse Polish Notation

#
[问题描述](https://leetcode.com/problems/evaluate-reverse-polish-notation)

[问题描述](../problems/)
这个问题是解析后缀表达式, 可以用栈来完成.

但要注意操作符对应的左右两侧的数值顺序.

## 代码实现

### Rust

```rust
{{#include src/main.rs:41:85}}
```

### C++

```cpp
{{#include main.cpp:5:}}
```
62 changes: 62 additions & 0 deletions src/leetcode/0150.evaluate-reverse-polish-notation/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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>
#include <string>
#include <vector>

int evalRPN(std::vector<std::string>& tokens) {
std::stack<int> stack;

for (const std::string& token : tokens) {
if (token == "+") {
const int num1 = stack.top();
stack.pop();
const int num2 = stack.top();
stack.pop();
const int num = num2 + num1;
stack.push(num);
} else if (token == "-") {
const int num1 = stack.top();
stack.pop();
const int num2 = stack.top();
stack.pop();
const int num = num2 - num1;
stack.push(num);
} else if (token == "*") {
const int num1 = stack.top();
stack.pop();
const int num2 = stack.top();
stack.pop();
const int num = num2 * num1;
stack.push(num);
} else if (token == "/") {
const int num1 = stack.top();
stack.pop();
const int num2 = stack.top();
stack.pop();
const int num = num2 / num1;
stack.push(num);
} else {
const int num = std::stoi(token);
stack.push(num);
}
}

// 栈顶的元素就是计算结果.
return stack.top();
}

void check_solution() {
std::vector<std::string> tokens = {"4","13","5","/","+"};
const int ret = evalRPN(tokens);
assert(ret == 6);
}

int main() {
check_solution();
return 0;
}
5 changes: 3 additions & 2 deletions src/leetcode/tags/stack/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ TODO:
## 中等

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)
2. [0150. 逆波兰表达式求值 Evaluate Reverse Polish Notation](../../0150.evaluate-reverse-polish-notation/index.md)
3. [0155. 最小栈 Min Stack](../../0155.min-stack/index.md)
4. [0394. 字符串解码 Decode String](../../0394.decode-string/index.md)

TODO:

Expand Down

0 comments on commit c81722f

Please sign in to comment.