Merge branch 'master' into master

This commit is contained in:
程序员Carl
2022-07-31 10:31:09 +08:00
committed by GitHub
192 changed files with 12743 additions and 1170 deletions

View File

@@ -10,7 +10,7 @@
# 20. 有效的括号
[力扣题目链接](https://leetcode-cn.com/problems/valid-parentheses/)
[力扣题目链接](https://leetcode.cn/problems/valid-parentheses/)
给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。
@@ -401,6 +401,7 @@ bool isValid(char * s){
}
```
C#:
```csharp
public class Solution {
@@ -432,5 +433,58 @@ public class Solution {
}
}
```
PHP:
```php
// https://www.php.net/manual/zh/class.splstack.php
class Solution
{
function isValid($s){
$stack = new SplStack();
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] == "(") {
$stack->push(')');
} else if ($s[$i] == "{") {
$stack->push('}');
} else if ($s[$i] == "[") {
$stack->push(']');
// 2、遍历匹配过程中发现栈内没有要匹配的字符 return false
// 3、遍历匹配过程中栈已为空没有匹配的字符了说明右括号没有找到对应的左括号 return false
} else if ($stack->isEmpty() || $stack->top() != $s[$i]) {
return false;
} else {//$stack->top() == $s[$i]
$stack->pop();
}
}
// 1、遍历完但是栈不为空,说明有相应的括号没有被匹配,return false
return $stack->isEmpty();
}
}
```
Scala:
```scala
object Solution {
import scala.collection.mutable
def isValid(s: String): Boolean = {
if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false
val stack = mutable.Stack[Char]()
// 循环遍历字符串
for (i <- s.indices) {
val c = s(i)
if (c == '(' || c == '[' || c == '{') stack.push(c)
else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false
// 以下三种情况不满足则直接返回false
else if(c==')' && stack.pop() != '(') return false
else if(c==']' && stack.pop() != '[') return false
else if(c=='}' && stack.pop() != '{') return false
}
// 如果为空则正确匹配,否则还有余孽就不匹配
stack.isEmpty
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>