LeetCode 20. Valid Parentheses

来源:互联网 发布:编程小白学 python 编辑:程序博客网 时间:2024/06/05 09:01
public class Solution {    public boolean isValid(String s) {        Stack<Character> stack = new Stack<Character>();        for (int i = 0; i < s.length(); i++) {        char c = s.charAt(i);        if (c == '(') stack.push(')');        else if (c == '[') stack.push(']');        else if (c == '{') stack.push('}');        else if (stack.isEmpty() || c != stack.pop()) return false;        }        if (stack.isEmpty()) return true;        else return false;    }}

0 0