zoukankan      html  css  js  c++  java
  • 0020. Valid Parentheses (E)

    Valid Parentheses (E)

    题目

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

    An input string is valid if:

    1. Open brackets must be closed by the same type of brackets.
    2. Open brackets must be closed in the correct order.

    Note that an empty string is also considered valid.

    Example 1:

    Input: "()"
    Output: true
    

    Example 2:

    Input: "()[]{}"
    Output: true
    

    Example 3:

    Input: "(]"
    Output: false
    

    Example 4:

    Input: "([)]"
    Output: false
    

    Example 5:

    Input: "([)]"
    Output: false
    

    题意

    给定一个只包含括号的字符串,判断括号是否匹配。

    思路

    用栈处理:是左半边括号则压入栈,是右半边括号则比较与栈顶是否匹配。最后检查栈是否清空。


    代码实现

    Java

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

    JavaScript

    /**
     * @param {string} s
     * @return {boolean}
     */
    var isValid = function (s) {
      let stack = []
    
      for (let c of s.split('')) {
        switch (c) {
          case '(':
          case '[':
          case '{':
            stack.push(c)
            break
          case ')':
            if (stack.length === 0 || stack.pop() !== '(') {
              return false
            }
            break
          case ']':
            if (stack.length === 0 || stack.pop() !== '[') {
              return false
            }
            break
          case '}':
            if (stack.length === 0 || stack.pop() !== '{') {
              return false
            }
            break
        }
      }
    
      return stack.length === 0
    }
    
  • 相关阅读:
    POJ 3694 Network (求桥,边双连通分支缩点,lca)
    UVA 796
    UVA 315 315
    POJ 1236 Network of Schools (有向图的强连通分量)
    【转】有向图强连通分量的Tarjan算法
    HDU 3072 Intelligence System (强连通分量)
    【转】强大的vim配置文件,让编程更随意
    WORDPRESS登录后台半天都无法访问或者是访问慢的解决方法
    phpstorm+Xdebug断点调试PHP
    PHP性能调优---PHP调试工具Xdebug安装配置教程
  • 原文地址:https://www.cnblogs.com/mapoos/p/13171278.html
Copyright © 2011-2022 走看看