Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
* 输入: "{[]}"
* 输出: true

# 思路
## 算法公开课

《代码随想录》算法视频公开课:[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频在看本篇题解,更有助于大家对链表的理解
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频再看本篇题解,更有助于大家对本题的理解**

## 思路

## 题外话
### 题外话

**括号匹配是使用栈解决的经典问题。**

Expand All @@ -68,7 +69,7 @@ cd a/b/c/../../

这里我就不过多展开了,先来看题。

## 进入正题
### 进入正题

由于栈结构的特殊性,非常适合做对称匹配类的题目。

Expand Down Expand Up @@ -143,8 +144,8 @@ public:

## 其他语言版本

### Java:

Java:
```Java
class Solution {
public boolean isValid(String s) {
Expand All @@ -171,7 +172,8 @@ class Solution {
}
```

Python:
### Python:

```python
# 方法一,仅使用栈,更省空间
class Solution:
Expand Down Expand Up @@ -213,7 +215,8 @@ class Solution:
return True if not stack else False
```

Go:
### Go:

```Go
func isValid(s string) bool {
hash := map[byte]byte{')':'(', ']':'[', '}':'{'}
Expand All @@ -235,7 +238,8 @@ func isValid(s string) bool {
}
```

Ruby:
### Ruby:

```ruby
def is_valid(strs)
symbol_map = {')' => '(', '}' => '{', ']' => '['}
Expand All @@ -253,7 +257,8 @@ def is_valid(strs)
end
```

Javascript:
### Javascript:

```javascript
var isValid = function (s) {
const stack = [];
Expand Down Expand Up @@ -296,7 +301,7 @@ var isValid = function(s) {
};
```

TypeScript:
### TypeScript:

版本一:普通版

Expand Down Expand Up @@ -348,7 +353,7 @@ function isValid(s: string): boolean {
};
```

Swift
### Swift:

```swift
func isValid(_ s: String) -> Bool {
Expand All @@ -373,7 +378,8 @@ func isValid(_ s: String) -> Bool {
}
```

C:
### C:

```C
//辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False
int notMatch(char par, char* stack, int stackTop) {
Expand Down Expand Up @@ -414,8 +420,8 @@ bool isValid(char * s){
}
```

### C#:

C#:
```csharp
public class Solution {
public bool IsValid(string s) {
Expand Down Expand Up @@ -447,7 +453,8 @@ public class Solution {
}
```

PHP:
### PHP:

```php
// https://www.php.net/manual/zh/class.splstack.php
class Solution
Expand Down Expand Up @@ -475,8 +482,8 @@ class Solution
}
```

### Scala:

Scala:
```scala
object Solution {
import scala.collection.mutable
Expand All @@ -499,7 +506,7 @@ object Solution {
}
```

rust:
### Rust:

```rust
impl Solution {
Expand Down
38 changes: 21 additions & 17 deletions problems/0150.逆波兰表达式求值.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>




> 这不仅仅是一道好题,也展现出计算机的思考方式

# 150. 逆波兰表达式求值
Expand Down Expand Up @@ -63,9 +61,13 @@

* 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。

# 思路
## 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解。
## 思路

### 正题

在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。

Expand Down Expand Up @@ -117,7 +119,7 @@ public:
* 空间复杂度: O(n)


## 题外话
### 题外话

我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。

Expand All @@ -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 {
Expand All @@ -164,7 +164,7 @@ class Solution {
}
```

python3
### Python3:

```python
from operator import add, sub, mul
Expand Down Expand Up @@ -201,7 +201,8 @@ class Solution:

```

Go:
### Go:

```Go
func evalRPN(tokens []string) int {
stack := []int{}
Expand All @@ -228,7 +229,7 @@ func evalRPN(tokens []string) int {
}
```

javaScript:
### JavaScript:

```js
var evalRPN = function (tokens) {
Expand Down Expand Up @@ -259,7 +260,7 @@ var evalRPN = function (tokens) {
};
```

TypeScript:
### TypeScript:

普通版:

Expand Down Expand Up @@ -324,7 +325,8 @@ function evalRPN(tokens: string[]): number {
};
```

Swift:
### Swift:

```Swift
func evalRPN(_ tokens: [String]) -> Int {
var stack = [Int]()
Expand Down Expand Up @@ -357,7 +359,8 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```

C#:
### C#:

```csharp
public int EvalRPN(string[] tokens) {
int num;
Expand Down Expand Up @@ -391,8 +394,8 @@ public int EvalRPN(string[] tokens) {
}
```

### PHP:

PHP:
```php
class Solution {
function evalRPN($tokens) {
Expand All @@ -417,7 +420,8 @@ class Solution {
}
```

Scala:
### Scala:

```scala
object Solution {
import scala.collection.mutable
Expand Down Expand Up @@ -447,7 +451,7 @@ object Solution {
}
```

rust:
### Rust:

```rust
impl Solution {
Expand Down
Loading