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 leetcode 20 http://fanyfull.github.io/2022/07/31/leetcode-20/ 作者 Fany Full 发布于 2022年7月31日 许可协议 VSCode 配置 C++ 单文件运行的方式 上一篇 leetcode 225 下一篇 Please enable JavaScript to view the comments