Lintcode423-Valid Parentheses-Easy
思路:
数据结构:stack。遍历整个字符串,如果遇到左向括号( [ { 则入栈。如果遇到右向括号时,先检查栈是否为空,为空说明左右向括号数目不一致,返回false;不为空则弹出栈顶元素查看是否和右向括号匹配。遍历完,要return stack.isEmpty(); 确保栈内没有剩余。
代码:
public class Solution {
/**
* @param s: A string
* @return: whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
Stack<Character> st = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == '[' || c == '{') {
st.push(c);
}
if (c == ')' || c == ']' || c == '}') {
if (st.isEmpty()) {
return false;
}
char popChar = st.pop();
if ( c == ')' && popChar != ')') {
return false;
}
if ( c == ']' && popChar != ']') {
return false;
}
if ( c == '}' && popChar != '}' ) {
return false;
}
}
}
return st.isEmpty();
}
}
代码风格优化:String遍历每一个char(line 9) ; 用if-else分支更准确(line 10, 12)
public class Solution {
/**
* @param s: A string
* @return: whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
Stack<Character> st = new Stack<Character>();
for (Character c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
st.push(c);
} else {
if (st.isEmpty()) {
return false;
}
char popChar = st.pop();
if ( c == ')' && popChar != '(') {
return false;
}
if ( c == ']' && popChar != '[') {
return false;
}
if ( c == '}' && popChar != '{') {
return false;
}
}
}
return st.isEmpty();
}
}
Lintcode423-Valid Parentheses-Easy的更多相关文章
- [leetcode] 20. Valid Parentheses (easy)
原题链接 匹配括号 思路: 用栈,遍历过程中,匹配的成对出栈:结束后,栈空则对,栈非空则错. Runtime: 4 ms, faster than 99.94% of Java class Solut ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)
709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...
- 46.Valid Parentheses
Valid Parentheses My Submissions QuestionEditorial Solution Total Accepted: 106346 Total Submissions ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- MIP是什么
MIP (Mobile Instant Pages - 移动网页加速器), 是一套应用于移动网页的开放性技术标准.通过提供MIP-HTML规范.MIP-JS运行环境以及MIP-Cache页面缓存系统, ...
- 【托业】【新东方托业全真模拟】TEST09~10-----P5~6
at no time 绝不,从不 takeover 收购 startup n.启动; 新兴公司(尤指新兴网络公司); 新兴公司,新开张的企业; specific具体的,特定的:factual 事实的, ...
- (转载)Java Map中的Value值如何做到可以为任意类型的值
转载地址:http://www.importnew.com/15556.html 如有侵权,请联系作者及时删除. 搬到我的博客来,有空细细品味,把玩. 本文由 ImportNew - shut ...
- java之数据库连接池-dbcp&c3p0&dbutils
介绍 因为数据库连接对象的创建比较消耗性能,所以可以在应用程序启动时就在内存中开辟一片空间(集合)存放多个数据库连接对象,后面需要连接时直接从该空间中取而不用新创建:使用完毕后归还连接(将连接重新放回 ...
- python框架之Django(4)-视图&路由
视图 负责接收请求和返回响应 FBV和CBV FBV FBV(function base views) 就是在视图里使用函数处理请求. from django.conf.urls import url ...
- 【UML】-NO.44.EBook.5.UML.1.004-【UML 大战需求分析】- 顺序图(Sequence Diagram)
1.0.0 Summary Tittle:[UML]-NO.44.EBook.1.UML.1.004-[UML 大战需求分析]- 顺序图(Sequence Diagram) Style:DesignP ...
- Cocos Creator cc.Node.点击事件
触摸事件 1.触摸事件的类型:START触摸启动,MOVED移动,ENDED弹起来,CANCEL取消; ENDED和CANCEL区别是ENDED物体内弹起来,CANCEL是在物体外范围弹起. 2.监听 ...
- vue2Leaflet使用 Vue2Leaflet-master 的demo
首先下载该demo 地址:https://github.com/KoRiGaN/Vue2Leaflet 下载后可以运行里面的例子,在examples文件夹内,该文件夹本身就是一个完整的项目 然后cmd ...
- 如何消除img默认的间距
方案一:div{font-size:0};方案二:img{ display:block};方案三:img{vertical-align:top;}方案四:div{ margin-bottom:-3px ...
- flask中单选、多选、下拉框的获取
1.单选: source = request.form.get('source') 2.多选: joy = request.form.getlist('joy') 或者 joy = re ...