1. 程式人生 > >LeetCode 20. Valid Parentheses 無效的括號匹配 Java易於理解的解法

LeetCode 20. Valid Parentheses 無效的括號匹配 Java易於理解的解法

題目

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.
另外,空字串也是有效的。

解題思路

不管哪種括號,必須只能被相同型別的括號關閉。換種角度來思考,那就是右括號只能和最近的和自己型別相同的左括號匹配。 這樣,我們再往下思考,只要把每次把最近放進去的括號匹配成功了那就是有效的字串。我們從前往後遍歷字串,然後選擇ArrayList來儲存當前位置的字元:如果當前字元是任意型別的左括號,那就是將當前字元新增到ArrayList中去;若果當前字元是任意型別的右括號,那就判斷它和ArrayList末尾的左括號是否匹配,如果匹配那就是把ArrayList末尾的字元刪除,如果不匹配那就說明返回false。

程式碼

Java程式碼如下:

    public boolean isValid(String s) {
        List<Character> list = new ArrayList<>();
        //空字串也是有效
        if (s.length() == 0)
            return true;
        //如果一開始就是右括號那就是無效字串
        if (s.charAt(0) == ')' || s.charAt(0) == ']' || s.charAt(0) == '}')
            return
false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') { list.add(s.charAt(i)); } else { if (s.charAt(i) == ')') { if (list.size()!=0&&list.get(list.size() - 1) == '(') { list.remove(list.size() - 1); } else { return false; } } else if (s.charAt(i) == ']') { if (list.size()!=0&&list.get(list.size() - 1) == '[') { list.remove(list.size() - 1); } else { return false; } } else if (s.charAt(i) == '}') { if (list.size()!=0&&list.get(list.size() - 1) == '{') { list.remove(list.size() - 1); } else { return false; } } } } //最後list是否全部匹配,全部匹配的話那麼list的大小就是0 if(list.size()!=0) return false; return true; }

相關推薦

LeetCode 20. Valid Parentheses 無效括號匹配 Java易於理解解法

題目 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. 給定一個只包含

LeetCode 20 Valid Parentheses(用棧判斷括號匹配

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

LeetCode 20. Valid Parentheses】(合法括號匹配判斷,棧的應用)

題目連結 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An in

LeetCode20 Valid Parentheses 有效括號

給定一個只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字串,判斷字串是否有效。 有效字串需滿足: 左括號必須用相同型別的右括號閉合。 左括號必須以正確的順序閉合。 注意空字串可被認為是有效字串。 示例 1: 輸入: “()” 輸出: true 示例 2: 輸入: “()[

leetcode-20 valid-parentheses(有效的括號

先看一下題目描述: 通過題目描述可以清楚的明白題目規則,輸出true或者false。這道題需要用借用棧來實現,不說理解直接放程式碼 1 public boolean isValidParentheses(String s) { 2 // Write your code her

LeetCode 20.Valid Parentheses (有效的括號)

題目描述: 給定一個只包括 '(',')','{','}','[',']' 的字串,判斷字串是否有效。 有效字串需滿足: 左括號必須用相同型別的右括號閉合。 左括號必須以正確的順序閉合。 注意空字串可被認為是有效字串。 示例: 輸入: "()" 輸出: tru

LeetCode 20Valid Parentheses(有效的括號)

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

[LeetCode]20 Valid Parentheses 有效的括號

parent desc ets must sam rip 而不是 ket 必須 [LeetCode]20 Valid Parentheses 有效的括號 Description Given a string containing just the characters ‘(

20. Valid Parentheses檢驗括號字符串的有效性

輸入 情況 brackets har 不存在 close ring HA IT [抄題]: Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine i

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

這個題果真很easy 但是注意“【”這種這有左邊的情況 class Solution { public boolean isValid(String s) { if(s.length()==0||s==null)return true;

Leetcode 20 Valid Parentheses

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

LeetCode easy專題】leetcode 20 Valid Parentheses(附c++ string介紹以及基於範圍的for迴圈簡介)

題目描述如下:(文末有string型別的介紹) 本題是迴文類題目中比較簡答的一種,輸入的字串也只有“(”、“)”、“["、”]“、”{“、”}“六種,題目可以產生一些變形,如判斷括號沒有閉合等,該類題目是面試中常考的題目,解決的方法通常採用stack(棧)這種資料結構,

[leetcode]-20. Valid Parentheses(C語言)

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

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