[leetcode]_Valid Parentheses
题目:判断给定字符串中的括号是否合法。题目中涉及三种符号'(' + ')' , '[' + ']' , '{' + '}'。
思路:利用stack来存储符号。
注意申请char型stack是: Stack<Character> op = new Stack<Character>();
stack.peek() 查看栈顶元素,但是不弹出;stack.pop() 查看栈顶元素,并弹出。
解题:
public boolean isValid(String s) {
int len = s.length();
Stack<Character> op = new Stack<Character>();
for(int i = 0 ; i < len ; i++){
char cur = s.charAt(i);
if(!op.isEmpty()){
char top = op.peek();
if(top == '(' && cur == ')' || top == '{' && cur == '}' || top == '[' && cur == ']') op.pop();
else op.push(cur);
}else{
op.push(cur);
}
}
return op.isEmpty();
}
轻松AC,咻~
[leetcode]_Valid Parentheses的更多相关文章
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
随机推荐
- 用jQuery之后,之前javascript的一些方法就不能用了吗
用jQuery之后,之前javascript的一些方法就不能用了吗? 比如$("#btn").onclick = function(){}这种用法?或者$("#btn&q ...
- 异步任务神器 Celery 简明笔记
转自:http://www.jianshu.com/p/1840035cb510 异步任务 异步任务是web开发中一个很常见的方法.对于一些耗时耗资源的操作,往往从主应用中隔离,通过异步的方式执行.简 ...
- 如何让你的Python代码更加pythonic ?
pythonic如果翻译成中文的话就是很python.很+名词结构的用法在中国不少. 以下为了简略,我们用P表示pythonic的写法,NP表示non-pythonic的写法,当然此P-NP非彼P-N ...
- 326.Power of Three
/* Given an integer, write a function to determine if it is a power of three. Follow up: Could you d ...
- Java是传值还是传引用
http://www.bccn.net/Article/kfyy/java/jszl/200601/3069.html 对于基本数据类型(整型.浮点型.字符型.布尔型等),传值;对于引用类型(对象.数 ...
- javascript 过滤字符串中的中文与空格
js 如何过滤字符串里中文或空格呢?方法有很多种,我们可以使用替换与正则表达式来实现,本文向大家介绍两个简单的例子,感兴趣的码农可以参考一下. 1.javascript过滤空格: function m ...
- [转]用man查看命令帮助时, 括号中的数字表示的意思
当提及如'rn(1)', 'ctime(3)'时,它们看上去像某种函数调用,但不是. 这些数字表示可在"Unix manual"中的哪一部分找到对应的文档. 数字对应的文档如下: ...
- LeetCode:237
题目:Delete Node in a Linked List(从列表中删除指定结点) 描述:Write a function to delete a node (except the tail) i ...
- C and C++ 如何嵌套使用
1. 要知道extern "C"的含义: (1) extern是C/C++中表明函数和全局变量作用范围的关键字, 该关键字表明其申明的函数和变量可以在本模块或者其他模块中使用. ( ...
- PERT(计划评审技术,Program Evaluation an Review Technique)
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 PERT(计划评审技术,Program Evaluation an Review T ...