Valid Parentheses
来源:互联网 发布:单片机应用技术 编辑:程序博客网 时间:2024/05/01 06:51
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.
//********** Hints ************
运用Stack即可
//*****************************
Solution:
public class Solution {
public boolean isValid(String s) {
if(s.length() == 0)
return true;
LinkedList<Character> stack = new LinkedList<Character>();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
switch(c){
case '(':
stack.add(c);
break;
case ')':
if(stack.isEmpty())
return false;
if(!stack.removeLast().equals('('))
return false;
break;
case '[':
stack.add(c);
break;
case ']':
if(stack.isEmpty())
return false;
if(!stack.removeLast().equals('['))
return false;
break;
case '{':
stack.add(c);
break;
case '}':
if(stack.isEmpty())
return false;
if(!stack.removeLast().equals('{'))
return false;
break;
default:
return false;
}
}
if(stack.isEmpty())
return true;
return false;
}
}
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- [leet code] Insertion Sort List
- Two Sum
- LeetCode - Combinations
- String to Integer
- 3Sum
- Valid Parentheses
- Merge Two Sorted List
- Implement strStr()
- 安装pvfs2
- Pow(x,n)
- Lesson 2: Udacity Intro to Parallel Programming
- liuru jia ACM训练指南链接(转载自BearChild blog)
- LeetCode - Merge k Sorted Lists
- 计算机视觉代码资源