【leetcode】第20题 Valid Parentheses 题目+解析+代码
来源:互联网 发布:杭州网络推广排名 编辑:程序博客网 时间:2024/06/05 06:58
【题目】
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 class Solution {public boolean isValid(String s) {char[] stack = new char[s.length()];int head = 0;char[] cs = s.toCharArray(); for(int i=0;i<cs.length;i++){ switch(cs[i]) {case '{':case '[':case '(':stack[head] = cs[i];head++;break;case '}':if(head == 0 || stack[head-1] != '{') return false; head--;break;case ')':if(head == 0 || stack[head-1] != '(') return false; head--;break;case ']':if(head == 0 || stack[head-1] != '[') return false;head--; break;}}return head == 0;}}
阅读全文
0 0
- 【leetcode】第20题 Valid Parentheses 题目+解析+代码
- 【leetcode】第32题 Longest Valid Parentheses 题目+解析+代码
- 【leetcode】第36题 Valid Sudoku 题目+解析+代码
- Leetcode 第20题 Valid Parentheses
- LeetCode第20题之Valid Parentheses
- valid parentheses leetcode 第20题
- leetcode 第20题<Valid Parentheses>(java)
- leetcode 第20-21题 Valid Parentheses & Generate Parentheses
- 24.leetcode题目20: Valid Parentheses
- 【LeetCode】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
- sapui5代码中使用jquey插件flot画图
- 设计模式--观察者
- 解决subline安装后无package control菜单项
- UVA1627TeamThemUp
- python爬虫学习第十六天——今天有点晚,请假一天
- 【leetcode】第20题 Valid Parentheses 题目+解析+代码
- 潘多拉盒子安装aria2命令
- 数据结构-线性表
- Codeforces Round #425 (Div. 2) C
- JavaScript属性值操作
- 个人学习过程:C#篇
- c++限制某个class所能产生对象数量的方法
- Maven--创建web
- PIP国内源