Difficulty level: Easy
Subject description:
Includes only a given '(', ')' string, '{', '}', '[', ']', and determine whether the string is valid.
Valid string must meet:
Left bracket must be closed by a closing parenthesis of the same type.
Left parenthesis must be closed in the correct order.
Note the empty string can be considered a valid string.
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
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/valid-parentheses
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Problem-solving ideas:
1, using the stack thought, traversing the left parenthesis added to the list
2. If you encounter a right parenthesis, and a pre-match and match brackets
3. If the match, that delete the brackets match, continue to traverse
Problem-solving Code:
class Solution: DEF isValid (Self, S: STR) -> BOOL: S = List (S) # into a list, because the string is immutable left_dics = [] # for storing the left bracket Rights = [ ' ) ' , ' ] ' , ' } ' ] paires = { ' ( ' : ' ) ' , ' [ ' : ' ] ' , ' {' : ' } ' } For char in S: IF char in paires: left_dics.append (char) # If left parenthesis, joined list elif char in Rights and left_dics: IF char == paires.get (left_dics [- 1]): # if the brackets on the right and in front of a relatively parenthesis match left_dics.pop () # if you delete the original list of matching last brace the else : BREAK the else : left_dics = 1 #If not match, out of the loop, this case is primarily exclude the first element of the anti-brackets situation BREAK IF not left_dics: return True the else : return False
Subject description:
Includes only a given '(', ')' string, '{', '}', '[', ']', and determine whether the string is valid.
Valid string must meet:
Left bracket must be closed by a closing parenthesis of the same type.
Left parenthesis must be closed in the correct order.
Note the empty string can be considered a valid string.
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
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/valid-parentheses
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Problem-solving ideas:
1, using the stack thought, traversing the left parenthesis added to the list
2. If you encounter a right parenthesis, and a pre-match and match brackets
3. If the match, that delete the brackets match, continue to traverse
Problem-solving Code:
class Solution: DEF isValid (Self, S: STR) -> BOOL: S = List (S) # into a list, because the string is immutable left_dics = [] # for storing the left bracket Rights = [ ' ) ' , ' ] ' , ' } ' ] paires = { ' ( ' : ' ) ' , ' [ ' : ' ] ' , ' {' : ' } ' } For char in S: IF char in paires: left_dics.append (char) # If left parenthesis, joined list elif char in Rights and left_dics: IF char == paires.get (left_dics [- 1]): # if the brackets on the right and in front of a relatively parenthesis match left_dics.pop () # if you delete the original list of matching last brace the else : BREAK the else : left_dics = 1 #If not match, out of the loop, this case is primarily exclude the first element of the anti-brackets situation BREAK IF not left_dics: return True the else : return False