leetcode 20

This problem is old sport. Hehe.

The draft paper:

import java.util.Stack;

public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push(')');
            } else if (s.charAt(i) == '[') {
                stack.push(']');
            } else if (s.charAt(i) == '{') {
                stack.push('}');
            } else if (!s.isEmpty() && s.charAt(i) == stack.peek()) {
                stack.pop();
            } else {
                return false;
            }
        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {
        var solu = new Solution();
        String s = "([{}])[]()";
        var res = solu.isValid(s);
        System.out.println(res);
    }
}

leetcode 20
http://fanyfull.github.io/2022/07/31/leetcode-20/
作者
Fany Full
发布于
2022年7月31日
许可协议