mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-13 14:10:38 +08:00
更新 0150.逆波兰表达式求值 排版格式修复
This commit is contained in:
@ -5,8 +5,6 @@
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
|
||||
|
||||
> 这不仅仅是一道好题,也展现出计算机的思考方式
|
||||
|
||||
# 150. 逆波兰表达式求值
|
||||
@ -63,9 +61,13 @@
|
||||
|
||||
* 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。
|
||||
|
||||
# 思路
|
||||
## 算法公开课
|
||||
|
||||
《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
### 正题
|
||||
|
||||
在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。
|
||||
|
||||
@ -117,7 +119,7 @@ public:
|
||||
* 空间复杂度: O(n)
|
||||
|
||||
|
||||
## 题外话
|
||||
### 题外话
|
||||
|
||||
我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。
|
||||
|
||||
@ -134,11 +136,9 @@ public:
|
||||
> During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and continued to use it in some models into the 2020s.
|
||||
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
java:
|
||||
### Java:
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
@ -164,7 +164,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
python3
|
||||
### Python3:
|
||||
|
||||
```python
|
||||
from operator import add, sub, mul
|
||||
@ -201,7 +201,8 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func evalRPN(tokens []string) int {
|
||||
stack := []int{}
|
||||
@ -228,7 +229,7 @@ func evalRPN(tokens []string) int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
var evalRPN = function (tokens) {
|
||||
@ -259,7 +260,7 @@ var evalRPN = function (tokens) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
普通版:
|
||||
|
||||
@ -324,7 +325,8 @@ function evalRPN(tokens: string[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
### Swift:
|
||||
|
||||
```Swift
|
||||
func evalRPN(_ tokens: [String]) -> Int {
|
||||
var stack = [Int]()
|
||||
@ -357,7 +359,8 @@ func evalRPN(_ tokens: [String]) -> Int {
|
||||
}
|
||||
```
|
||||
|
||||
C#:
|
||||
### C#:
|
||||
|
||||
```csharp
|
||||
public int EvalRPN(string[] tokens) {
|
||||
int num;
|
||||
@ -391,8 +394,8 @@ public int EvalRPN(string[] tokens) {
|
||||
}
|
||||
```
|
||||
|
||||
### PHP:
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
function evalRPN($tokens) {
|
||||
@ -417,7 +420,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Scala:
|
||||
### Scala:
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
import scala.collection.mutable
|
||||
@ -447,7 +451,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
|
Reference in New Issue
Block a user