Question:

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Analysis:

问题描述:给出一个字符串,包含'('')''{''}''[' and ']'确定它是否是有效地匹配括号。

思路:一看到括号匹配问题肯定想到用栈。遇到左括号就进栈;遇到右括号若栈顶元素与之匹配则POP出栈顶元素,若不匹配则返回false。

注意特殊情况:如"{}[]()", 或者“{[}]”, 或者“{{}}{{”等情况。

Answer:

public class Solution {
public boolean isValid(String s) {
char[] ch = s.toCharArray();
int n = ch.length;
if(ch.length == 0 || ch.length % 2 != 0)
return false;
if(ch[0] == '}' || ch[0] == ']' || ch[0] == ')')
return false;
Stack<Character> st = new Stack<Character>();
st.add(ch[0]);
int i = 1;
while(!st.isEmpty() && i < n) {
if(ch[i] == '{' || ch[i] == '[' || ch[i] == '(') {
st.add(ch[i]);
i++;
} else {
if(st.isEmpty())
return false;
char c = st.pop();
if(c == '{' && ch[i] != '}' || c == '[' && ch[i] != ']'
|| c == '(' && ch[i] != ')')
return false;
i++;
if(i < n && (ch[i] == '{' || ch[i] == '[' || ch[i] == '(')) {
st.add(ch[i]);
i++;
}
}
}
System.out.println(i +" " +st.size());
if(!st.isEmpty() || i < n - 1)
return false; return true;
} }

LeetCode -- Valid Parenthese的更多相关文章

  1. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  2. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  3. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  4. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  6. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  7. [LeetCode] Valid Parentheses 验证括号

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

  8. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. LeetCode——Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

随机推荐

  1. 利用Kettle转储接口数据

    1.     项目背景 1.1.  项目背景 数据接口 API:应用程序接口(Application Program Interface)的简称,是实现计算机软件之间数据通信的工具.同时API也是一种 ...

  2. Kettle报表自动化

    来自我们牛逼哄哄的东哥的笔记 1.   2. 3. 选择数据库链接 贴报表SQL 4. 文件名:选择路径,excel文件由kettle自动创建,自己只需输入创建文件的名称. 拓展名:后缀写上 5. 此 ...

  3. ant Design表单验证笔记

    1.pattern正则验证 <Col md={12} sm={24}> <FormItem {...formItemLayout} label="班数"> ...

  4. Gulp工具

    Gulp是一个基于node开发的构建工具. gulp本身是不进行任何构建任务,是通过gulp的一些列插件完成: gulp-less  编译LESS文件: gulp-autoprefix  添加css私 ...

  5. JS常用数组方法及实例

    1.join(separator):将数组的元素组起一个字符串,以separator为分隔符 ,,,,]; var b = a.join("|"); //如果不用分隔符,默认逗号隔 ...

  6. R-biomaRt使用-代码备份

    目标:使用R脚本从ensembl上下载transcript数据 简单粗暴,直接上代码.biomaRt的介绍晚一点更新. # this file helps extract information fr ...

  7. dialog BLE SDK 学习(3)

    dialog DA14580 SDK版本:5.0.4. 本文介绍了DA14580 SDK的学习感想和建议,分享给大家. 首先,Dialog官网上的资料挺全的,但是是英文的,如果英文不好,比如笔者,阅读 ...

  8. vue 组件间数据传递

    父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...

  9. android stadio 快捷键最好的材料 android stadio大全 最牛逼的android stadio快捷键

    一: .nn .if .for .toast .instanceof .switch 这些都是可以直接点的,一个变量然后.for ,如果这个这个变量是个集合,都可以.for 二: 关闭所有窗口 ctr ...

  10. Linux命令学习总结(一)

    命令 -选项 参数 如果选项是一个单词时,选项前面要加2个- modprobe -r pcspkr   在终端中输入的时候有声音,可以用这个命令屏蔽声音 ,需要root权限 useradd userd ...