Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f1776a

Browse files
committed
style: update question #20
1 parent ca06981 commit 1f1776a

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_20/java/Solution.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hi.dhl.algorithms.leetcode._20.java;
22

33
import java.util.ArrayDeque;
4+
import java.util.Deque;
45

56
/**
67
* <pre>
@@ -63,8 +64,8 @@
6364

6465
class Solution {
6566
public boolean isValid(String s) {
66-
ArrayDeque<Character> stack = new ArrayDeque<Character>();
67-
// 遍历字符串
67+
Deque<Character> stack = new ArrayDeque<Character>();
68+
// 开始遍历字符串
6869
for (int i = 0; i < s.length(); i++) {
6970
char c = s.charAt(i);
7071
// 遇到左括号,则将其对应的右括号压入栈中
@@ -75,17 +76,13 @@ public boolean isValid(String s) {
7576
} else if (c == '{') {
7677
stack.push('}');
7778
} else {
78-
// 当前栈为空,直接返回 false
79-
if (stack.isEmpty()) {
80-
return false;
81-
}
82-
// 当前右括号,与栈顶元素不相等,不相等直接返回 false
83-
char tmp = stack.poll();
84-
if (c != tmp) {
79+
// 遇到右括号,判断当前元素是否和栈顶元素相等,不相等提前返回,结束循环
80+
if (stack.isEmpty() || stack.poll() != c) {
8581
return false;
8682
}
8783
}
8884
}
85+
// 通过判断栈是否为空,来检查是否是有效的括号
8986
return stack.isEmpty();
9087
}
9188
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_20/kotlin/Solution.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ import java.util.*
1313
class Solution {
1414
fun isValid(s: String): Boolean {
1515
val stack = ArrayDeque<Char>()
16-
// 遍历字符串
16+
// 开始遍历字符串
1717
for (c in s) {
1818
when (c) {
19-
// 遇到左括号,则将其对应的右括号压入栈中
19+
// 遇到左括号,将对应的右括号压入栈中
2020
'(' -> stack.push(')')
2121
'[' -> stack.push(']')
2222
'{' -> stack.push('}')
2323
else -> {
24-
// 当前右括号,与栈顶元素不相等,不相等直接返回 false
25-
val tmp = stack.poll()
26-
if (c != tmp) {
27-
return false;
24+
// 遇到右括号,判断当前元素是否和栈顶元素相等,不相等提前返回,结束循环
25+
if (stack.isEmpty() || stack.poll() != c) {
26+
return false
2827
}
2928
}
3029
}
3130
}
32-
return stack.isEmpty();
31+
// 通过判断栈是否为空,来检查是否是有效的括号
32+
return stack.isEmpty()
3333
}
3434
}
3535

‎leetcode/stack/02-valid-parentheses.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ Example 5:
3636
Output: true
3737
```
3838

39-
### 题目解析
39+
### 算法流程
4040

41-
1. 遍历字符串
42-
2. 遇到左括号,则将其对应的右括号压入栈中
43-
3. 如果遇到右括号:
44-
* 当前栈为空,直接返回false;
45-
* 当前右括号对应的左括号,与栈顶元素不相等,直接返回false
46-
4. 重复执行 步骤 2 和步骤 3
47-
5. 循环结束之后,判断栈是否为空,不为空返回false
41+
![](http://img.hi-dhl.com/16315437955394.gif)
42+
43+
1. 如果遇到左括号,将对应的右括号压入栈中
44+
2. 如果遇到右括号
45+
* 判断当前栈是否为空
46+
* 如果不为空,判断当前元素是否和栈顶元素相等
47+
* 如果不相等,发现了不符合的括号,提前返回 `false`,结束循环
48+
3. 重复执行「步骤 1」 和「步骤 2」
49+
4. 循环结束之后,通过判断栈是否为空,来检查是否是有效的括号
50+
51+
**复杂度分析**
52+
53+
假设字符串的长度为 `N` 则:
54+
55+
* 时间复杂度:`O(N)`。正确有效的括号需要遍历了一次字符串,所需要的时间复杂度为 `O(N)`
56+
* 空间复杂度:`O(N)`。如果输入字符串全是左括号,例如 `(((((((`,栈的大小即为输入字符串的长度,所需要的空间复杂度为 `O(N)`
4857

4958
<!-- tabs:start -->
5059

@@ -54,23 +63,23 @@ Example 5:
5463
class Solution {
5564
fun isValid(s: String): Boolean {
5665
val stack = ArrayDeque<Char>()
57-
// 遍历字符串
66+
// 开始遍历字符串
5867
for (c in s) {
5968
when (c) {
60-
// 遇到左括号,则将其对应的右括号压入栈中
69+
// 遇到左括号,将对应的右括号压入栈中
6170
'(' -> stack.push(')')
6271
'[' -> stack.push(']')
6372
'{' -> stack.push('}')
6473
else -> {
65-
// 当前右括号,与栈顶元素不相等,不相等直接返回 false
66-
val tmp = stack.poll()
67-
if (c != tmp) {
68-
return false;
74+
// 遇到右括号,判断当前元素是否和栈顶元素相等,不相等提前返回,结束循环
75+
if (stack.isEmpty() || stack.poll() != c) {
76+
return false
6977
}
7078
}
7179
}
7280
}
73-
return stack.isEmpty();
81+
// 通过判断栈是否为空,来检查是否是有效的括号
82+
return stack.isEmpty()
7483
}
7584
}
7685
```
@@ -80,8 +89,8 @@ class Solution {
8089
```
8190
class Solution {
8291
public boolean isValid(String s) {
83-
ArrayDeque<Character> stack = new ArrayDeque<Character>();
84-
// 遍历字符串
92+
Deque<Character> stack = new ArrayDeque<Character>();
93+
// 开始遍历字符串
8594
for (int i = 0; i < s.length(); i++) {
8695
char c = s.charAt(i);
8796
// 遇到左括号,则将其对应的右括号压入栈中
@@ -92,17 +101,13 @@ class Solution {
92101
} else if (c == '{') {
93102
stack.push('}');
94103
} else {
95-
// 当前栈为空,直接返回 false
96-
if (stack.isEmpty()) {
97-
return false;
98-
}
99-
// 当前右括号,与栈顶元素不相等,不相等直接返回 false
100-
char tmp = stack.poll();
101-
if (c != tmp) {
104+
// 遇到右括号,判断当前元素是否和栈顶元素相等,不相等提前返回,结束循环
105+
if (stack.isEmpty() || stack.poll() != c) {
102106
return false;
103107
}
104108
}
105109
}
110+
// 通过判断栈是否为空,来检查是否是有效的括号
106111
return stack.isEmpty();
107112
}
108113
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /