鱼C论坛

 找回密码
 立即注册
查看: 1559|回复: 0

[学习笔记] leetcode 20. Valid Parentheses

[复制链接]
发表于 2019-8-30 01:27:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

  2. An input string is valid if:

  3. Open brackets must be closed by the same type of brackets.
  4. Open brackets must be closed in the correct order.
  5. Note that an empty string is also considered valid.

  6. Example 1:

  7. Input: "()"
  8. Output: true
  9. Example 2:

  10. Input: "()[]{}"
  11. Output: true
  12. Example 3:

  13. Input: "(]"
  14. Output: false
  15. Example 4:

  16. Input: "([)]"
  17. Output: false
  18. Example 5:

  19. Input: "{[]}"
  20. Output: true
复制代码

  1. class Solution {
  2.     public boolean isValid(String s) {
  3.         
  4.         if(s == null || s.length() == 0) return true;
  5.         
  6.         if(s.length() == 1) return false;
  7.         
  8.         int len = s.length();
  9.         
  10.         Stack <Character> st = new Stack<>();
  11.         
  12.         for(int i = 0; i< len; i++){
  13.             
  14.             if(st.empty() && (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}')) return false;
  15.             
  16.             if(s.charAt(i) == '(') st.push(')');
  17.             
  18.             if(s.charAt(i) == '[') st.push(']');
  19.             
  20.             if(s.charAt(i) == '{') st.push('}');
  21.             
  22.             if(s.charAt(i) == ')' && st.peek() == ')') st.pop();
  23.             else if(s.charAt(i) == ')') st.push(')');
  24.                
  25.             if(s.charAt(i) == ']' && st.peek() == ']') st.pop();
  26.             else if(s.charAt(i) == ']') st.push(']');
  27.             
  28.             if(s.charAt(i) == '}' && st.peek() == '}') st.pop();
  29.             else if(s.charAt(i) == '}') st.push('}');
  30.         }
  31.         
  32.         if(st.empty()){
  33.             
  34.             return true;
  35.         }
  36.         
  37.         return false;
  38.         
  39.     }
  40. }
复制代码

  1. class Solution {
  2.     public boolean isValid(String s) {
  3.         
  4.         // if(s == null || s.length() == 0) return true;
  5.         
  6.         // if(s.length() == 1) return false;
  7.         
  8.         int len = s.length();
  9.         
  10.         Stack <Character> st = new Stack<>();
  11.         
  12.         for(int i = 0; i< len; i++){
  13.             
  14.             if(s.charAt(i) == '(') st.push(')');
  15.             
  16.             else if(s.charAt(i) == '[') st.push(']');
  17.             
  18.             else if(s.charAt(i) == '{') st.push('}');
  19.             
  20.             else if (st.empty() || st.pop() != s.charAt(i)){
  21.                
  22.                 return false;
  23.             }
  24.         }
  25.         
  26.         return st.empty();
  27.         
  28.     }
  29. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 08:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表