LeetCode 678. 有效的括号字符串
题目描述
思路分析
正反遍历
参考代码
1
write your code here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean checkValidString(String s) {
int left = 0, right = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == '*') {
left++;
} else {
right++;
}
if (right > left) {
return false;
}
}
left = 0;
right = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
if (c == ')' || c == '*') {
right++;
} else {
left++;
}
if (left > right) {
return false;
}
}
return true;
}
}
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用