zoukankan      html  css  js  c++  java
  • leetcode — valid-parentheses

    import java.util.Stack;
    
    /**
     * Source : https://oj.leetcode.com/problems/valid-parentheses/
     *
     * Created by lverpeng on 2017/7/11.
     *
     * * Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
     * determine if the input string is valid.
     *
     * The brackets must close in the correct order, "()" and "()[]{}" are all valid
     * but "(]" and "([)]" are not.
     *
     */
    public class ValiadParentheses {
    
        /**
         * 括弧匹配,使用栈进行判断,遇到左括号入栈,遇到右括号判断出栈,如果匹配则出栈,不匹配返回
         * 最后判断是否匹配完成,并判断栈是否为空
         *
         * @param str
         * @return
         */
        public boolean valiad (String str) {
            Stack<Character> stack = new Stack<Character>();
            for (int i = 0; i  < str.length(); i++) {
                char c = str.charAt(i);
                if (c == '(' || c == '[' || c == '{') {
                    stack.push(c);
                } else if (c == ')' || c == ']' || c == '}') {
                    char pop = stack.pop();
                    if (!((pop == '(' && c == ')') || (pop == '[' && c == ']') || (pop == '{' && c == '}'))) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            return stack.empty();
        }
    
    
        public static void main(String[] args) {
            ValiadParentheses valiadParentheses = new ValiadParentheses();
            System.out.println("true-------" + valiadParentheses.valiad("{}[]()"));
            System.out.println("true-------" + valiadParentheses.valiad("{[()]}[]()"));
            System.out.println("true-------" + valiadParentheses.valiad("{}[({})]()"));
            System.out.println("false-------" + valiadParentheses.valiad("{}[[()]"));
            System.out.println("false-------" + valiadParentheses.valiad("{}[][(]"));
            System.out.println("false-------" + valiadParentheses.valiad("{}2[][]()[]"));
    
        }
    
    
    }
    
  • 相关阅读:
    Thymeleaf模板引擎语法
    kali更新软件源
    解决kali安装成功后没有声音的问题
    SSO的误区及建议
    关于 target="_blank"漏洞的分析
    好久没来了,平时一些笔记都记在印象笔记,长传一波
    BIOS基础
    CSRF的本质及防御
    linux下stricky
    CSRF与xss的区别
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7368386.html
Copyright © 2011-2022 走看看