https://leetcode-cn.com/problems/valid-parentheses/

    1. class Solution {
    2. public boolean isValid(String s) {
    3. LinkedList<Character> stack = new LinkedList<>();
    4. // 遍历字符串中所有字符,依次判断
    5. for (int i = 0; i < s.length(); i++) {
    6. char c = s.charAt(i);
    7. // 判断当前是左/右括号
    8. if (c == '(') {
    9. stack.push(')');
    10. } else if (c == '[') {
    11. stack.push(']');
    12. } else if (c == '{') {
    13. stack.push('}');
    14. } else {
    15. if (stack.isEmpty() || stack.pop() != c) return false;
    16. }
    17. }
    18. return stack.isEmpty();
    19. }
    20. }