mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
更新 020.有效的括号 排版格式修复
This commit is contained in:
@ -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),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
## 题外话
|
||||
### 题外话
|
||||
|
||||
**括号匹配是使用栈解决的经典问题。**
|
||||
|
||||
@ -68,7 +69,7 @@ cd a/b/c/../../
|
||||
|
||||
这里我就不过多展开了,先来看题。
|
||||
|
||||
## 进入正题
|
||||
### 进入正题
|
||||
|
||||
由于栈结构的特殊性,非常适合做对称匹配类的题目。
|
||||
|
||||
@ -143,8 +144,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
public boolean isValid(String s) {
|
||||
@ -171,7 +172,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
# 方法一,仅使用栈,更省空间
|
||||
class Solution:
|
||||
@ -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{')':'(', ']':'[', '}':'{'}
|
||||
@ -235,7 +238,8 @@ func isValid(s string) bool {
|
||||
}
|
||||
```
|
||||
|
||||
Ruby:
|
||||
### Ruby:
|
||||
|
||||
```ruby
|
||||
def is_valid(strs)
|
||||
symbol_map = {')' => '(', '}' => '{', ']' => '['}
|
||||
@ -253,7 +257,8 @@ def is_valid(strs)
|
||||
end
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
var isValid = function (s) {
|
||||
const stack = [];
|
||||
@ -296,7 +301,7 @@ var isValid = function(s) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
版本一:普通版
|
||||
|
||||
@ -348,7 +353,7 @@ function isValid(s: string): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
Swift
|
||||
### Swift:
|
||||
|
||||
```swift
|
||||
func isValid(_ s: String) -> Bool {
|
||||
@ -373,7 +378,8 @@ func isValid(_ s: String) -> Bool {
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
|
||||
```C
|
||||
//辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False
|
||||
int notMatch(char par, char* stack, int stackTop) {
|
||||
@ -414,8 +420,8 @@ bool isValid(char * s){
|
||||
}
|
||||
```
|
||||
|
||||
### C#:
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public class Solution {
|
||||
public bool IsValid(string s) {
|
||||
@ -447,7 +453,8 @@ public class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
### PHP:
|
||||
|
||||
```php
|
||||
// https://www.php.net/manual/zh/class.splstack.php
|
||||
class Solution
|
||||
@ -475,8 +482,8 @@ class Solution
|
||||
}
|
||||
```
|
||||
|
||||
### Scala:
|
||||
|
||||
Scala:
|
||||
```scala
|
||||
object Solution {
|
||||
import scala.collection.mutable
|
||||
@ -499,7 +506,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
|
Reference in New Issue
Block a user