先看一下题目描述:

通过题目描述可以清楚的明白题目规则,输出true或者false。这道题需要用借用栈来实现,不说理解直接放代码

 1 public boolean isValidParentheses(String s) {
 2         // Write your code here
 3         Stack<Character> stack = new Stack<>();
 4         for (char c : s.toCharArray()) {
 5             if (c == '(' || c == '[' || c == '{') {
 6                 stack.push(c);
 7             }
 8             if (c == ')') {
 9                 if (stack.isEmpty() || stack.pop() != '(') {
10                     return false;
11                 }
12             }
13             if (c == ']') {
14                 if (stack.isEmpty() || stack.pop() != '[') {
15                     return false;
16                 }
17             }
18             if (c == '}') {
19                 if (stack.isEmpty() || stack.pop() != '{') {
20                     return false;
21                 }
22             }
23         }
24         return stack.isEmpty();
25     }
26 }

还有一种进阶版,代码更少,但是思路更犀利,不太容易想到,同样借助栈

 1 public boolean isValid(String s) {
 2         Stack<Character> stack = new Stack<>();
 3         for(char c:s.toCharArray()){
 4             if(c=='('){
 5                 stack.push(')');
 6             }
 7             if(c=='{'){
 8                 stack.push('}');
 9             }
10             if(c=='['){
11                 stack.push(']');
12             }
13             else if(stack.isEmpty()||stack.pop()!=c){
14                 return false;
15             }
16         }
17         return stack.isEmpty();
18     }

其实两段代码思路一模一样,非常巧妙

转载于:https://www.cnblogs.com/qingshan0216/p/9994952.html

leetcode-20 valid-parentheses(有效的括号)相关推荐

  1. LeetCode: 20. Valid Parentheses

    0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...

  2. 20. Valid Parentheses 有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...

  3. [LeetCode]--20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. LeetCode 20 Valid Parentheses (C++)

    问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  5. [swift] LeetCode 20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. LeetCode 20. Valid Parentheses(c++)

    利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...

  7. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

    题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...

  8. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  9. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  10. LeetCode 20. Valid Parenthese

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

最新文章

  1. MFC消息处理学习总结
  2. 在哪开启oracle服务器,开启企业殿堂的钥匙 Oracle服务器的安装
  3. Azure DevOps Server (TFS) 代码库Repo管理培训
  4. Feature Extraction
  5. 中国人工智能学会通讯——人工智能在各医学亚专科的发展现状及趋势 1.6 结束语...
  6. 托马斯微积分第十一版_企业微服务第一部分
  7. 报名系统 服务器,全国人事考试服务平台系统报名步骤(带报名入口)
  8. Ogre 天龙八部地形管理器(2) --- 地形和静态对象的加载
  9. html调用ckplayer说明,CKplayer功能配置(示例代码)
  10. 通用发票在线OCR识别,报销场景适用,支持近20种票据
  11. 质量免费--读书笔记(上篇)
  12. Softmax-with-Loss层的计算图 | Softmax梯度推导 | Loss损失函数
  13. 算法的时间复杂度表示
  14. 十六进制颜色值 (美观必备 - 设置颜色)
  15. GameofMir引擎架设传奇服务器【4:架设微端】
  16. gurobi中的lp
  17. Hexo博客icarus主题定制篇
  18. PUCCH(3)matlab验证ZC序列的性质
  19. Java-万物皆对象
  20. python星座属相查询_python的生肖和星座计算函数

热门文章

  1. 第五周周记(国庆第五天)
  2. 我爱自然语言处理bert ner chinese
  3. 最全Pycharm教程(43)——Pycharm扩展功能之UML类图使用 代码结构
  4. javaSE8的流库总结
  5. Http请求之优雅的RestTemplate
  6. LeetCode简单题之截断句子
  7. 云计算灾备原理与预防恢复方案
  8. OpenCL编程详细解析与实例
  9. Python:Downloader Middlewares
  10. Centos7下安装MongoDB