LeetCode -- Valid Parenthese
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的更多相关文章
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode——Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别。
DROP: DROP TABLE test; 删除表test,并释放空间,将test删除的一干二净. TRUNCATE: TRUNCATE test; 删除表test里的内容,并释放空间,但不删除表的 ...
- 面向对象封装的web服务器
import socket import re import os import sys # 由于前面太繁琐,可以用类封装一下,也可以分几个模块 class HttpServer(object): d ...
- java数组之二分法查找
认识: 猜字游戏 步数 所猜的数 结果 可能值的范围 0 1~100 1 50 太高 1~49 2 25 太低 26~49 3 37 太高 26~36 4 31 太低 32~36 5 34 太 ...
- 用js控制单选框或者多选框问题
出现如图问题时,这时不能用attr方法添加checked属性了,改用$( "input" ).prop( "checked", true ),完美解决.
- RESTful API架构和oauth2.0认证机制(概念版)
1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...
- POJ 3662 (二分+SPFA
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8856 Accepted: 3211 D ...
- POJ:2785-4 Values whose Sum is 0(双向搜索)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 26974 Accepted: ...
- POJ:2100-Graveyard Design(尺取)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 8504 Accepted: 2126 Cas ...
- python基础之布尔运算、集合
布尔值 True 真 False 假 所有的数据类型都自带布尔值,数据只有在0,None和空的时候为False. print(bool()) print(bool()) print(bool('')) ...
- JavaSE总结--面向对象
封装: 1)为什么要用private修饰 保护属性或方法不被别人随意调用. 继承: 继承模型: 多态: 接口: 抽象类: 内部类: 在编译时用$分隔. 访问局部变量,该变量必须用final修饰. 向下 ...