-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/leetcode/0150.evaluate-reverse-polish-notation/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
src/leetcode/0150.evaluate-reverse-polish-notation/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters