20. Valid Parentheses
来源:互联网 发布:5轴激光切割编程软件 编辑:程序博客网 时间:2024/05/16 19:02
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.
Subscribe to see which companies asked this question
也是上古时期做的题了……其实现在要做就是一个stack的事,看到左括号就入栈,右括号就出栈,看是不是相对应的就好。以前的这种方法可以弃了。
public class Solution {<span style="white-space:pre"></span>public boolean isValid(String s) {<span style="white-space:pre"></span>String str = "";<span style="white-space:pre"></span>int len = s.length();<span style="white-space:pre"></span>for (int i = 0; i < len; i++) {<span style="white-space:pre"></span>// if(str.length()==0&&(s.charAt(i)==')'||s.charAt(i)=='}'||s.charAt(i)==']'))return<span style="white-space:pre"></span>// false;<span style="white-space:pre"></span>if (s.charAt(i) == '(')<span style="white-space:pre"></span>str += '(';<span style="white-space:pre"></span>if (s.charAt(i) == ')') {<span style="white-space:pre"></span>if (str.length() == 0 || str.charAt(str.length() - 1) != '(')<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>else<span style="white-space:pre"></span>str = str.substring(0, str.length() - 1);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (s.charAt(i) == '[')<span style="white-space:pre"></span>str += '[';<span style="white-space:pre"></span>if (s.charAt(i) == ']') {<span style="white-space:pre"></span>if (str.length() == 0 || str.charAt(str.length() - 1) != '[')<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>else<span style="white-space:pre"></span>str = str.substring(0, str.length() - 1);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (s.charAt(i) == '{')<span style="white-space:pre"></span>str += '{';<span style="white-space:pre"></span>if (s.charAt(i) == '}') {<span style="white-space:pre"></span>if (str.length() == 0 || str.charAt(str.length() - 1) != '{')<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>else<span style="white-space:pre"></span>str = str.substring(0, str.length() - 1);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (str.length() == 0)<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>}}
0 0
- LeetCode 20. Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- 20.Valid Parentheses
- 20.Valid Parentheses
- 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- [leetcode] 20.Valid Parentheses
- [leetcode] 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- 20. Valid Parentheses
- 20. Valid Parentheses
- Problem C: 统计不及格人数
- 图数据库Neo4j介绍
- 总结CSS中清除浮动的三种方法
- Sublime优美设置(待续)
- 给tomcat7w.exe改名字
- 20. Valid Parentheses
- 关于HTML初识 笔记
- 用Python直接写UTF-8文本文件
- python数据类型--序列
- android开发步步为营之97:几种常用的android图片内存优化方法避免OOM
- Problem D: 求最高同学位置
- 19. Remove Nth Node From End of List
- 58. Length of Last Word
- mac与windows系统的互相访问