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.

public boolean isValid(String s) {if (s.length() % 2 != 0)return false;char[] characters = new char[s.length()];int index = 0, i;for (i = 0; i < s.length(); i++) {if (s.charAt(i) == '(')characters[index++] = s.charAt(i);if (s.charAt(i) == '[')characters[index++] = s.charAt(i);if (s.charAt(i) == '{') {characters[index++] = s.charAt(i);System.out.println(index);}if (s.charAt(i) == ')') {if (--index < 0 || characters[index] != '(')break;}if (s.charAt(i) == ']') {if (--index < 0 || characters[index] != '[')break;}if (s.charAt(i) == '}') {if (--index < 0 || characters[index] != '{')break;}}if (i == s.length() && index == 0)return true;return false;}

方法通过了,不过觉得有点笨,暂时也没想到好的,先就这样啦。LeedCode也没给我们提供详解。

找到一个写得比较好的程序,极力推荐大家不要看我的,看这个,又感觉被甩了几条街。

public boolean isValidParentheses(String s) {Stack<Character> stack = new Stack<Character>();for (Character c : s.toCharArray()) {if ("({[".contains(String.valueOf(c))) {stack.push(c);} else {if (!stack.isEmpty() && is_valid(stack.peek(), c)) {stack.pop();} else {return false;}}}return stack.isEmpty();}private boolean is_valid(char c1, char c2) {return (c1 == '(' && c2 == ')') || (c1 == '{' && c2 == '}')|| (c1 == '[' && c2 == ']');}

用了系统的栈,我用的数组栈。

[LeetCode]--20. Valid Parentheses相关推荐

  1. LeetCode: 20. Valid Parentheses

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

  2. LeetCode 20 Valid Parentheses (C++)

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

  3. [swift] LeetCode 20. Valid Parentheses

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

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

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

  5. LeetCode Longest Valid Parentheses

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

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

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

  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

    Question Given a string containing just the characters '(' and ')', find the length of the longest v ...

  9. LeetCode 20. Valid Parenthese

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

最新文章

  1. 近7000字长文详细讲解Java包装类,面试稳了
  2. ORA-28000: the account is locked 的解决办法!
  3. php no input file specified.,nginx+php出现No input file specified解决办法
  4. python和arduino串口通信_利用串行通信实现python与arduino的同步
  5. 台式机计算机怎么分割,编辑手把手教程 如何给电脑硬盘分区
  6. Linux 系统的运行级别(Run Level)
  7. linux安装opencv让输入密码,Linux下安装OpenCV步骤
  8. 大型网站架构系列:缓存在分布式系统中的应用
  9. 基于大数据的房价数据可视化分析预测系统
  10. 视频编码格式——h264优点
  11. 机器人算法工程师入门指南(三)机器人算法工程师需要学习哪些知识?(及教材推荐)
  12. 利用Google博客搜索查看加密QQ空间(qzone)日志
  13. 问农事 - 菜蔬作物的节令
  14. cmake rpath Makefile rapth
  15. 为什么运放一般要反比例放大?
  16. 硬件描述语言(HDL)
  17. 冯诺依曼 计算机名言,约翰·冯·诺伊曼留给我们的名言之一
  18. 【Django毕业设计源码】Python考试题库练习系统
  19. 《富兰克林自传》修身
  20. 平面3连杆机器人正逆解

热门文章

  1. 云栖专辑 | 阿里开发者们的第6个感悟:享受折磨
  2. 查找域内所有的Windows Server 2012 R2的服务器,并区分出哪些是物理机,那些是虚拟机...
  3. 前端应该掌握的网络知识(1)
  4. IT人的自我导向型学习:学习的4个层次
  5. 英语中十二个月份的由来
  6. Android开发学习笔记:浅谈Content Provider
  7. ASP.NET性能优化之构建自定义文件缓存
  8. 树莓派 Raspberry Pi 更换国内源
  9. (一)使用appium之前为什么要安装nodejs???
  10. 进程共享变量#pragma data_seg用法