Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
自己尝试的代码:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
public
class
Solution {
public
boolean
isValid(String s) {
if
(s.length()==
0
){
return
true
;
}
//新建一个int类型的数字用来辅助判断,数组中(代表1,[代表2,{代表3
int
[] num =
new
int
[s.length()];
int
i =
0
;
//遍历每个字符
for
(
int
j=
0
;j<s.length();j++){
boolean
flag =
false
;
switch
(s.charAt(j)){
//数组中(代表1,[代表2,{代表3
case
'('
:
num[i++] =
1
;
break
;
case
'['
:
num[i++] =
2
;
break
;
case
'{'
:
num[i++] =
3
;
break
;
default
:
if
(i!=
0
){
flag =
true
;
switch
(s.charAt(j)){
//数组中)代表-1,]代表-2,}代表-3
case
')'
:
num[--i] -=
1
;
break
;
case
']'
:
num[--i] -=
2
;
break
;
case
'}'
:
num[--i] -=
3
;
break
;
default
:
return
false
;
}
}
else
{
return
false
;
}
}
//是否存在符号不匹配
if
(i>=
0
&&flag==
true
&&num[i]!=
0
){
return
false
;
}
}
//最后是否存在单个括号
if
(num[
0
]!=
0
){
return
false
;
}
return
true
;
}
}
|
改进的代码:
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
|
public
class
Solution {
public
boolean
isValid(String s) {
//通过HashMap将括号的对应关系存起来
HashMap<Character,Character> map =
new
HashMap<Character,Character>();
map.put(
'('
,
')'
);
map.put(
'['
,
']'
);
map.put(
'{'
,
'}'
);
//使用栈来存储右括号,当有相应的左括号相邻出现时,出栈,否则返回false
Stack<Character> stack =
new
Stack<Character>();
for
(
int
i=
0
;i<s.length();i++){
char
cur = s.charAt(i);
if
(map.containsKey(cur)){
stack.push(cur);
}
else
if
(!stack.empty()&&map.get(stack.peek())==cur){
stack.pop();
}
else
{
return
false
;
}
}
//当栈中存在残留的右括号时,说明没有相应的左括号,返回false,否则返回true
return
stack.empty();
}
}
|
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
自己尝试的代码:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
public
class
Solution {
public
boolean
isValid(String s) {
if
(s.length()==
0
){
return
true
;
}
//新建一个int类型的数字用来辅助判断,数组中(代表1,[代表2,{代表3
int
[] num =
new
int
[s.length()];
int
i =
0
;
//遍历每个字符
for
(
int
j=
0
;j<s.length();j++){
boolean
flag =
false
;
switch
(s.charAt(j)){
//数组中(代表1,[代表2,{代表3
case
'('
:
num[i++] =
1
;
break
;
case
'['
:
num[i++] =
2
;
break
;
case
'{'
:
num[i++] =
3
;
break
;
default
:
if
(i!=
0
){
flag =
true
;
switch
(s.charAt(j)){
//数组中)代表-1,]代表-2,}代表-3
case
')'
:
num[--i] -=
1
;
break
;
case
']'
:
num[--i] -=
2
;
break
;
case
'}'
:
num[--i] -=
3
;
break
;
default
:
return
false
;
}
}
else
{
return
false
;
}
}
//是否存在符号不匹配
if
(i>=
0
&&flag==
true
&&num[i]!=
0
){
return
false
;
}
}
//最后是否存在单个括号
if
(num[
0
]!=
0
){
return
false
;
}
return
true
;
}
}
|
改进的代码:
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
|
public
class
Solution {
public
boolean
isValid(String s) {
//通过HashMap将括号的对应关系存起来
HashMap<Character,Character> map =
new
HashMap<Character,Character>();
map.put(
'('
,
')'
);
map.put(
'['
,
']'
);
map.put(
'{'
,
'}'
);
//使用栈来存储右括号,当有相应的左括号相邻出现时,出栈,否则返回false
Stack<Character> stack =
new
Stack<Character>();
for
(
int
i=
0
;i<s.length();i++){
char
cur = s.charAt(i);
if
(map.containsKey(cur)){
stack.push(cur);
}
else
if
(!stack.empty()&&map.get(stack.peek())==cur){
stack.pop();
}
else
{
return
false
;
}
}
//当栈中存在残留的右括号时,说明没有相应的左括号,返回false,否则返回true
return
stack.empty();
}
}
|