【Leetcode Algorithm】Valid Parentheses



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();
     }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值