Leetcode#20||Valid Parentheses

来源:互联网 发布:二手知豆电动汽车价格 编辑:程序博客网 时间:2024/06/07 02:53


public class Solution {    public boolean isValid(String s) {        Map<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 c = s.charAt(i);                        if (map.keySet().contains(c)) {                stack.push(c);            } else if (map.values().contains(c)) {                if (!stack.isEmpty() && map.get(stack.peek()) == c) {                    stack.pop();                } else {                    return false;                }            }        }               return stack.isEmpty();         }}


0 0
原创粉丝点击