【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
題意:給定一個由括號字元組成的字串,問是否合法。
思路:
最簡單的思路,遍歷判斷入棧,最後判斷棧是否為空。
Code:
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char ch : s.toCharArray()) {
if (ch == '(') {
stack.push(')');
} else if (ch == '[') {
stack.push(']');
} else if (ch == '{') {
stack.push('}');
} else if (stack.empty() || stack.pop() != ch) {///棧為空或者棧頂不等於ch
return false;
}
}
return stack.empty();
}
public static void main(String[] args) {
String str = "" + "()";
boolean res = new Solution().isValid(str);
System.out.println(res);
}
}
相關推薦
【LeetCode 20. Valid Parentheses】(合法括號匹配判斷,棧的應用)
題目連結 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An in
Leetcod--20. Valid Parentheses(極簡潔的括號匹配)
bsp for NPU peek pan color bool put tco Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if the i
【LeetCode easy專題】leetcode 20 Valid Parentheses(附c++ string介紹以及基於範圍的for迴圈簡介)
題目描述如下:(文末有string型別的介紹) 本題是迴文類題目中比較簡答的一種,輸入的字串也只有“(”、“)”、“["、”]“、”{“、”}“六種,題目可以產生一些變形,如判斷括號沒有閉合等,該類題目是面試中常考的題目,解決的方法通常採用stack(棧)這種資料結構,
LeetCode 20 Valid Parentheses(用棧判斷括號匹配)
Given a string containing just the characters'(',')','{','}','['and']', determine if the input str
[leetcode]-20. Valid Parentheses(C語言)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is
LeetCode 20. Valid Parentheses 時間複雜度(O( n))
時間複雜度(O( n)) class Solution { public: bool isValid(string s) { if(s.length()%2==1)return false; vector<char&
LeetCode - 20. Valid Parentheses(0ms)
string out etc contain not The leetcode for side Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine
LeetCode 20. Valid Parentheses
true shm bracket .get 匹配 brush imp charat nta Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if
#Leetcode# 20.Valid Parentheses
https://leetcode.com/problems/valid-parentheses/description/ Given a string containing just the characters '(', ')', '{', '}',
[leetcode] 20. Valid Parentheses (easy)
原題連結 匹配括號 思路: 用棧,遍歷過程中,匹配的成對出棧;結束後,棧空則對,棧非空則錯。 Runtime: 4 ms, faster than 99.94% of Java class Solution { public boolean isValid(String s) {
leetcode-20 valid-parentheses(有效的括號)
先看一下題目描述: 通過題目描述可以清楚的明白題目規則,輸出true或者false。這道題需要用借用棧來實現,不說理解直接放程式碼 1 public boolean isValidParentheses(String s) { 2 // Write your code her
[leetcode]20. Valid Parentheses
這個題果真很easy 但是注意“【”這種這有左邊的情況 class Solution { public boolean isValid(String s) { if(s.length()==0||s==null)return true;
LeetCode 20.Valid Parentheses (有效的括號)
題目描述: 給定一個只包括 '(',')','{','}','[',']' 的字串,判斷字串是否有效。 有效字串需滿足: 左括號必須用相同型別的右括號閉合。 左括號必須以正確的順序閉合。 注意空字串可被認為是有效字串。 示例: 輸入: "()" 輸出: tru
Leetcode 20 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input
LeetCode 20 — Valid Parentheses(有效的括號)
Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input str
LeetCode 20. Valid Parentheses 無效的括號匹配 Java易於理解的解法
題目 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. 給定一個只包含
19.1.29 [LeetCode 20] Valid Parentheses
opened [] 技術分享 har pop closed show display top Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine i
leetcode 20-Valid Parentheses
原題: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The bracke
[C語言][LeetCode][20]Valid Parentheses
題目 Valid Parentheses Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string
[LeetCode]20 Valid Parentheses 有效的括號
parent desc ets must sam rip 而不是 ket 必須 [LeetCode]20 Valid Parentheses 有效的括號 Description Given a string containing just the characters ‘(