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. IntelliJ IDEA 新版发布:支持CPU火焰图,新增酷炫主题

    JetBrain 是一家伟大的公司,一直致力于为开发者开发世界上最好用的集成开发环境 就在上周,JetBrain 公司发布了 Java 集成开发环境 IntelliJ IDEA 最新版本 2018.3 ...

  2. 解决 windows下安装Anaconda后python pip不可用的情况

    在windows系统下通过安装Anaconda的方式安装的python使用中发现不能再通过pip安装python包.只能通过conda install packname 的方法,导致很多conda不支 ...

  3. WinFormEx

    项目地址 :  https://github.com/kelin-xycs/WinFormEx WinFormEx 一个 用 C# 写的 WinForm 扩展库 , 用于改善 WinForm 的 界面 ...

  4. Postgresql导出数据报版本不对

    zabbix使用得数据库是Postgresql,最近zabbix4.0版本出来了,准备把zabbix升级,得先把数据库备份,但是一直报错,如下:     查找服务器上是否有10的版本,也一直没找到   ...

  5. Day 05 可变不可变、数据类型内置方法

    1.可变类型:值改变,但是id不变,证明就是改变原值,是可变类型 2.不可变类型:值改变,但是id也跟着改变,证明是产生新的值,是不可变类型 数字类型 一.整型int 1.用途:记录年龄.等级.数量 ...

  6. select 查询

    使用as给字段起别名,例如:select name as 姓名 from student; 模糊匹配(like) "_":一个占位符.例子:select * from studen ...

  7. golang垃圾回收和SetFinalizer

    golang自带内存回收机制--GC.GC通过独立的进程执行,它会搜索不再使用的变量,并释放.需要注意的是,进行GC会占用机器资源. GC是自动进行的.如果要手动进行GC,可以调用runtime.GC ...

  8. linux git 保存用户名和密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  9. IDEA—— 找不到或无法加载主类Main

    最近使用idea,编写了一个项目,发现老是找不到main,网上找了一大圈的解决方案,都不行.灵机一动升级了jdk就可以了,之前用的是1.7的,换成了1.8的就好了.

  10. npm安装与使用

    NPM 使用介绍 摘自:http://www.runoob.com/nodejs/nodejs-npm.html NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题, ...