Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

题意:

给定一个括号序列,判断其是否合法。

思路:

指针i来扫给定字符串

对于字符串的每个char,若是左括号,入栈

若栈不为空&&栈顶元素与对应位置的右括号匹配,出栈

代码:

 class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(c == '(' || c =='{' || c=='['){
stack.push(c);
}
else if( c == ')' && !stack.empty() && stack.peek() =='('){
stack.pop();
}
else if( c == '}' && !stack.empty() && stack.peek() =='{'){
stack.pop();
}
else if( c == ']' && !stack.empty() && stack.peek() =='['){
stack.pop();
}
else{
return false;
}
}
return stack.isEmpty();
}
}

followup:  Valid Parentheses 简化版:只有()一种括号,且input string里有别的字母,加减号。看括号是否是闭合。

)()()() ----> true
(+1^$#)(#$) ----> true
)( ----->false
(()#%33 ----->false

代码:

 public class valid_parenthese_modified {
public boolean isValid(String s) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == '(')
count++;
else if (c == ')') {
if (count == 0) // notes for the if-judge here
return false;
count--;
}
}
return count == 0;
}
}

[leetcode]20. Valid Parentheses有效括号序列的更多相关文章

  1. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. [LeetCode]20. Valid Parentheses有效的括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  7. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  8. leetcode 20 Valid Parentheses 有效的括号

    描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...

  9. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

随机推荐

  1. git grep的一些用法

    https://www.kernel.org/pub/software/scm/git/docs/git-grep.html   把所有本地分支包含某个字符的行列出来,把含有master的列出来 gi ...

  2. Windows Azure Virtual Network (13) 跨数据中心之间的虚拟网络点对点连接VNet Peering

    <Windows Azure Platform 系列文章目录> 今天是大年初二,首先祝大家新年快乐,万事如意. 在笔者之前的文章中:Windows Azure Virtual Networ ...

  3. jenkins构建触发器详解-不登录触发远程构建详解

    利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...

  4. 对中断interrupt的理解

    一.中断 线程的几种状态:新建.就绪.运行.阻塞.死亡.参考:线程的几种状态转换 线程的可运行状态并不代表线程一定在运行(runnable != running ) . 大家都知道:所有现代桌面和服务 ...

  5. Pymysql部分

    安装: 1 执行SQL import pymysql # 创建连接 conn = pymysql.connect(host='172.30.2.233', port=3306, user='root' ...

  6. android selector shape 使用

    先上效果图 message_toolbar_left_bg_selector <?xml version="1.0" encoding="utf-8"?& ...

  7. 网络之TCP握手总结

    目录: 31.Tcp握手的一些问题? 21.Tcp三次握手及SYN攻击: 四次握手? 为什么建立连接是三次握手,而关闭连接却是四次挥手? 13.TCP释放连接四次握手 12.TCP建立连接三次握手 1 ...

  8. springboot 的war包在Tomcat中启动失败

    springboot 默认是通常是打包成jar的,里面会内置一个tomcat容器 有时候我们需要使用以前打成war包的方式部署到对应的tomcat中, 具体springboot 怎么从jar改成war ...

  9. 汉化-PowerDesigner 16.5 汉化

    转载: https://www.cnblogs.com/yeaicc/p/PowerDesigner16CN.html 一.背景 经常使用PowerDesigner,之前使用15版本,后来16出来后, ...

  10. MySQL视图-(视图创建,修改,删除,查看,更新数据)

    视图是一种虚拟存在的表,对于使用视图的用户来说基本上是透明的.视图并不在数据库中实际存在,行和列数据来自定义视图的查询总使用的表,并且是在使用视图时动态生成的. 视图相对于普通表的优势: 简单:使用视 ...