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. windows server 2008 R2之tomcat开机自启

    方法一: 写一个批处理文件autostartup.bat用来启动tomcat,内容如下.复制时不要把复制内容也复制进去 set CATALINA_HOME=C:\apache-tomcat-8.5.3 ...

  2. Dynamics 365 CRM 添加自定义按钮

    在添加自定义按钮之前,我们需要下载这个工具 RibbonWorkbench, 它是专门针对自定义命令栏和Ribbon区域. 下载之后是一个zip压缩包. 怎样安装RibbonWorkbench: Se ...

  3. Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found

    Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found 网上大部分文章都是说在bios开启vt-x支持等.这里 ...

  4. DNS基础

    什么是DNS? DNS--Domain name system,域名系统,简单来说就是域名和IP地址间的映射关系.当你在浏览器的地址栏输入网址(或域名,如 www,baidu.com)的时候,在网络中 ...

  5. django路由系统URLS

    usrls: from django.contrib import admin from django.urls import path from cmbd import views from dja ...

  6. pycharm同步代码

    有时候我们需要在windows环境开发代码,但是程序要跑在linux服务器或者我们的linux虚拟机上,这就设计到了代码同步的问题. pycharm提供了代码同步的功能. 在导航栏点击Tools--D ...

  7. 对poi-excel导出的浅层理解

    上一篇对excel导入做了浅层的解释,本文将对导出再做浅层解释. 仍然是相同的套路,只不过是反过来而已. 反过来方向理论上本来是这样的:cell-->row-->sheet-->wo ...

  8. Cglib源码分析 invoke和invokeSuper的差别(转)

    原文 https://blog.csdn.net/makecontral/article/details/79593732 Cglib的实例 本文重在源码的分析,Cglib的使用不再复述. //被代理 ...

  9. 20165312 2017-2018-2 《JAVA程序设计》第2周学习总结

    20165312 2017-2018-2 <JAVA程序设计>第2周学习总结 一.对上一周学习的查漏补缺 1.上周在虚拟机中进行编译程序时出现错误,在上一周的博客中我有提到,当时还未找到解 ...

  10. .NET自动化测试工具链:Selenium+NUnit+ExtentReport

    Selenium可以执行UI的交互,ExtentReport用来生成测试报告,NUnit是我熟悉的基础测试框架,当然你也可以用MSTest.Xunit来代替.Selenium.NUnit没啥好讲的,网 ...