其他分享
首页 > 其他分享> > LeetCode20-括号的匹配

LeetCode20-括号的匹配

作者:互联网

 

// bobo老师
import java.util.Stack;
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }
            else{
                if (stack.isEmpty()){
                    return false;
                }
                char topChar = stack.pop();
                if (c == ')' && topChar != '('){
                    return false;
                }
                if (c == ']' && topChar != '['){
                    return false;
                }
                if (c == '}' && topChar != '{'){
                    return false;
                }
            }
        }
        return stack.isEmpty();

    }
}
Soultion 1

 

标签:return,&&,匹配,stack,括号,Stack,topChar,false,LeetCode20
来源: https://www.cnblogs.com/HuangYJ/p/12819802.html