#20 Valid Parentheses
来源:互联网 发布:windows 桌面路径 编辑:程序博客网 时间:2024/06/07 18:34
题目:
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.
考察栈的基本使用,简单的入栈和出栈即可解决。刚看到了一种好玩的for循环格式,这里用一下。
public class Solution { public boolean isValid(String s) { Stack<Character> stackS = new Stack<Character>(); char[] arr = s.toCharArray(); for (char c : arr) { if(stackS.isEmpty()){ stackS.push(c); continue; } Character temp = stackS.pop(); // 配对时c不入栈 if (temp == '[' && c == ']') { } <span style="white-space:pre"></span> else if (temp == '(' && c == ')') {<span style="white-space:pre"></span> }<span style="white-space:pre"></span> else if (temp == '{' && c == '}') {<span style="white-space:pre"></span> }<span style="white-space:pre"></span> <span style="white-space:pre"></span> <span style="white-space:pre"></span> // 不配对时c入<span style="white-space:pre"></span> else {<span style="white-space:pre"></span> stackS.push(temp);<span style="white-space:pre"></span> stackS.push(c);<span style="white-space:pre"></span> } } return stackS.isEmpty(); }}
0 0
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- 20、Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- LeetCode | #20 Valid Parentheses
- leetcode.20-----------Valid Parentheses
- [leetcode] 20 Valid Parentheses
- leetcode-20 Valid Parentheses
- leetCode #20 Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode 20 Valid Parentheses
- LeetCode 20 - Valid Parentheses
- #20 Valid Parentheses
- LeetCode 20: Valid Parentheses
- 【STL源码剖析读书笔记】【第8章】配接器
- spring的原理
- Web services之Axis
- 编译错误 在msvcrtd.lib(libcmt.lib)中重定义
- 栈和堆
- #20 Valid Parentheses
- Apache MiNa 实现多人聊天室(好文,收藏)
- PyQt4 将QTDesigner的界面 在py中展示
- Ubuntu 安装mysql
- linux c 文件操作汇总
- OGNL
- Second Highest Salary
- 用cytoscape.js展示neo4j网络关系图 - 2. py2neo
- 一行代码不用敲,15分钟轻松搞定GIS开发,GISer福音,教你如何从下载到使用webappbuilder创建第一个应用程序(不用申请有使用期限的企业账号哦!)