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 ...
随机推荐
- java学习之路--String类方法的应用
消除字符串两端的空格 1.判断字符串第一个位置是否为空格,如果是继续向下判断,直到不是空格位置,末尾也是这样,往前判断,直到不是空格为止. 2.当开始和末尾都不是空格时,获取字符串. public s ...
- devmapper: Thin Pool has 154464 free data blocks which is less than minimum required 163840 free dat
清理exited进程: docker rm $(docker ps -q -f status=exited) 清理dangling volumes: docker volume rm $(docker ...
- Spring-Boot 内置静态资源文件地址修改
Spring-Boot 内置MVC静态文件地址修改 Why:1.Spring-Boot修改内置SpringMVC静态资源路径,提高项目目录结构的安全性.2.配置拦截路径时可以剔除静态文件拦截How:1 ...
- vue的数据双向绑定和ref获取dom节点
vue是一个MVVM的框架 业务逻辑代码即js部分是model部分, html是view部分. 当model改变的时候,view也会改变 view 改变是,model也会改变 <template ...
- 自定制property
class Lazyproperty: def __init__(self, func): self.func = func def __get__(self, instance, owner): p ...
- [本体论][UML][统一建模语言][软件建模][OWL]从本体论到UML到OWL
以下内容,是关于软件建模的方法与思路. UML与OWL都是基于本体论的建模语言. 本体论(哲学) 本体论(信息科学) UML(统一建模语言) more info 参考:[设计语言][统一建模语言][软 ...
- https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版
#!/usr/bin/env python # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apach ...
- stl string的erase方法
; i < s.size(); i++) { ') { s.erase(i,); i--; } } 删除string中的所有0.
- python基础语法及知识点总结
本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...
- smartctl 检测磁盘信息
smartctl -i 指定设备 -d 指定设备类型,例如:ata, scsi, marvell, sat, 3ware,N -a 或A 显示所有信息 -l 指定日志的类型,例如:TYPE: err ...