20.Valid Parentheses
程序员文章站
2024-03-22 16:18:58
...
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> characterStack = new Stack<>();
int len = s.length();
Map<Character, Character> map = new HashMap<>();
map.put(')', '(');
map.put(']', '[');
map.put('}', '{');
for(int i = 0; i < len; ++i) {
if( characterStack.isEmpty()) {
characterStack.push(s.charAt(i));
continue;
}
Character c = s.charAt(i);
Character t = characterStack.peek();
if(t == map.get(c)) characterStack.pop();
else characterStack.push(c);
}
return characterStack.isEmpty();
}
//
// public static void main(String[] args) {
// Solution solution = new Solution();
// System.out.println(solution.isValid("()"));
// }
}
上一篇: 回文数判断(Leetcode)
下一篇: 剑指Offer之判断一个数是否是回文数
推荐阅读
-
20.Valid Parentheses
-
20.Valid Parentheses
-
Leetcode - 20.Valid Parentheses
-
Generate Parentheses 生成括号 LeetCode 22
-
LeetCode 22:括号生成(Generate Parentheses)解法汇总
-
LeetCode刷题:20. Valid Parentheses
-
[leetcode] Longest Valid Parentheses
-
hdu 6400 Parentheses Matrix
-
hdu6400 Parentheses Matrix 构造
-
LC22 General Parentheses 回溯/递归 生产所有有效的括号对