Leetcode 20 Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- 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
這個題是簡單的字串匹配
1)
class Solution {
public boolean isValid(String s) {
if(s == null || s.length() < 1){
return true;
}else if(s.length() == 1){
return false;
}
Stack<Character> stack = new Stack<>();
for(int i = 0 ; i < s.length();i++){
if(s.charAt(i) != ')' && s.charAt(i) != ']' && s.charAt(i) != '}'){
stack.push(s.charAt(i));
}else{
if(!stack.isEmpty() && stack.peek() == '(' && s.charAt(i) == ')'){
stack.pop();
}else if(!stack.isEmpty() && stack.peek() == '[' && s.charAt(i) == ']'){
stack.pop();
}else if(!stack.isEmpty() && stack.peek() == '{' && s.charAt(i) == '}'){
stack.pop();
}else{
return false;
}
}
}
if(!stack.isEmpty()){
return false;
}else{
return true;
}
}
}
2)
class Solution {
public boolean isValid(String s) {
int top = -1;
char[] cs = s.toCharArray();
for(int i=0;i<s.length();i++){
if(top < 0 || !ism(cs[top], cs[i])){
top++;
cs[top] = cs[i];// 如果只有一種括號就不需要這行
}
else
top--;
}
return top == -1;
}
public boolean ism(char c1, char c2){
if(c1 == '(' && c2 ==')')
return true;
if(c1 == '[' && c2 ==']')
return true;
if(c1 == '{' && c2 =='}')
return true;
return false;
}
}
相關文章
- Leetcode 20 有效的括號valid-parentheses(棧)LeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- LeetCode Valid Parentheses(020)解法總結LeetCode
- LeetCode 之 JavaScript 解答第20題 —— 有效的括號(Valid Parentheses)LeetCodeJavaScript
- Longest Valid Parentheses
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- Leetcode 22 Generate ParenthesesLeetCode
- Leetcode - 022. Generate ParenthesesLeetCode
- Leetcode 36 Valid SudokuLeetCode
- LeetCode Generate Parentheses(022)解法總結LeetCode
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- [LeetCode] 678. Valid Parenthesis StringLeetCode
- leetcode 593. Valid Square練習LeetCode
- leetcode 593. Valid Square 練習LeetCode
- 【leetcode】22. Generate Parentheses 合法括號串的所有組合LeetCode
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- LeetCode | 20 ValidParenthesesLeetCode
- PHP-Leetcode-20PHPLeetCode
- 22. Generate Parentheses (recursion algorithm)Go
- Leetcode——20. 有效的括號LeetCode
- Leetcode20. 有效的括號LeetCode
- 【LeetCode】 20.有效的括號LeetCode
- LeetCode 20. 有效的括號LeetCode
- 036 Valid Sudoku
- spring - mvc - @ValidSpringMVC
- 125. Valid Palindrome
- 65-Valid Number
- LeetCode-20. 有效的括號(棧模擬)LeetCode
- CSS :valid 選擇器CSS
- fatal: Not a valid object name: 'master'ObjectAST
- Please provide a valid cache pathIDE
- InnoDB: No valid checkpoint found.
- gipchaLowerProcessNode: no valid interfaces found to node
- 941. Valid Mountain ArrayAI
- await is only valid in async functionAIFunction
- 技術乾貨:LeetCode1- 20題詳解LeetCode
- Caused by: Error: ' ' is not a valid resource name characterError