diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 14357348..a8e57bbe 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/leetcode/0150.evaluate-reverse-polish-notation/.gitignore b/src/leetcode/0150.evaluate-reverse-polish-notation/.gitignore new file mode 100644 index 00000000..95811e00 --- /dev/null +++ b/src/leetcode/0150.evaluate-reverse-polish-notation/.gitignore @@ -0,0 +1 @@ +/main diff --git a/src/leetcode/0150.evaluate-reverse-polish-notation/Makefile b/src/leetcode/0150.evaluate-reverse-polish-notation/Makefile new file mode 100644 index 00000000..b686101f --- /dev/null +++ b/src/leetcode/0150.evaluate-reverse-polish-notation/Makefile @@ -0,0 +1,3 @@ + +build: main.cpp + g++ -g main.cpp -o main diff --git a/src/leetcode/0150.evaluate-reverse-polish-notation/index.md b/src/leetcode/0150.evaluate-reverse-polish-notation/index.md index 77a618ba..32b1b360 100644 --- a/src/leetcode/0150.evaluate-reverse-polish-notation/index.md +++ b/src/leetcode/0150.evaluate-reverse-polish-notation/index.md @@ -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:}} +``` \ No newline at end of file diff --git a/src/leetcode/0150.evaluate-reverse-polish-notation/main.cpp b/src/leetcode/0150.evaluate-reverse-polish-notation/main.cpp new file mode 100644 index 00000000..a11c7b2a --- /dev/null +++ b/src/leetcode/0150.evaluate-reverse-polish-notation/main.cpp @@ -0,0 +1,62 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +#include + +#include +#include +#include + +int evalRPN(std::vector& tokens) { + std::stack 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 tokens = {"4","13","5","/","+"}; + const int ret = evalRPN(tokens); + assert(ret == 6); +} + +int main() { + check_solution(); + return 0; +} diff --git a/src/leetcode/tags/stack/index.md b/src/leetcode/tags/stack/index.md index 4ed8a1b7..fac4af07 100644 --- a/src/leetcode/tags/stack/index.md +++ b/src/leetcode/tags/stack/index.md @@ -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: