演算法Valid Parentheses
點選(此處)摺疊或開啟
-
public static boolean isValid(String s) {
-
HashMap<Character, Character> map = new HashMap<Character, Character>();
-
map.put(\'(\', \')\');
-
map.put(\'[\', \']\');
-
map.put(\'{\', \'}\');
-
-
Stack<Character> stack = new Stack<Character>();
-
-
for (int i = 0; i < s.length(); i++) {
-
char curr = s.charAt(i);
-
-
if (map.keySet().contains(curr)) {
-
stack.push(curr);
-
} else if (map.values().contains(curr)) {
-
if (!stack.empty() && map.get(stack.peek()) == curr) {
-
stack.pop();
-
} else {
-
return false;
-
}
-
}
-
}
-
-
return stack.empty();
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29012686/viewspace-1153230/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Longest Valid Parentheses
- Leetcode 20 Valid ParenthesesLeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- LeetCode Valid Parentheses(020)解法總結LeetCode
- Leetcode 20 有效的括號valid-parentheses(棧)LeetCode
- LeetCode 之 JavaScript 解答第20題 —— 有效的括號(Valid Parentheses)LeetCodeJavaScript
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- Leetcode 22 Generate ParenthesesLeetCode
- Leetcode - 022. Generate ParenthesesLeetCode
- 22. Generate Parentheses (recursion algorithm)Go
- LeetCode Generate Parentheses(022)解法總結LeetCode
- 036 Valid Sudoku
- spring - mvc - @ValidSpringMVC
- Leetcode 36 Valid SudokuLeetCode
- 125. Valid Palindrome
- 65-Valid Number
- 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
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- [LeetCode] 678. Valid Parenthesis StringLeetCode
- Caused by: Error: ' ' is not a valid resource name characterError
- A valid provisioning profile for this executable was not found.
- @Valid和@Validated的區別
- @Valid 與 @Validated 的區別
- leetcode 593. Valid Square練習LeetCode
- leetcode 593. Valid Square 練習LeetCode
- Java中@Valid子物件註釋Java物件
- openssl_private_encrypt(): key param is not a valid
- 【leetcode】22. Generate Parentheses 合法括號串的所有組合LeetCode
- Docker 警告 Plugin XXX is not valid: failed to fetch metadataDockerPluginAI
- SSL - SSLHandshakeException: unable to find valid certification path to requested targetException
- @Validated、@Valid在service層引數校驗
- 報錯“Please indicate a valid Swagger or OpenAPI version field”SwaggerAPI