• Valid Parentheses


    package cn.edu.xidian.sselab.string;

    import java.util.Stack;

    /**
     *
     * @author zhiyong wang
     * title: Valid Parentheses
     * content:
     *     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 ValidParentheses {

        //这个题既然是成对出现的,第一个想到的就应该是栈,成对用栈的出入来表示,只要是匹配成功就弹栈,这样栈最后就为空,弹的元素一定是与之匹配的元素
        public boolean isValid(String s){
            int len = s.length();
            if(s == null || len < 2) return false;
            Stack stack = new Stack();
            char temp;
            for(int i=0;i<len;i++){
                if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{')
                    stack.push(s.charAt(i));
                if(s.charAt(i) == ')'){
                    if(stack.isEmpty() || (char) stack.pop() != '(') return false;
                }
                if(s.charAt(i) == ']'){
                    if(stack.isEmpty() || (char) stack.pop() != '[') return false;
                }
                if(s.charAt(i) == '}'){
                    if(stack.isEmpty() || (char) stack.pop() != '{') return false;
                }
            }
            return stack.isEmpty();
        }
    }

  • 相关阅读:
    设计模式1 设计模式概述
    关于jdk的配置
    搭建webpack项目框架
    移动乐淘day1
    前后端开发(2):浏览器与PHP程序的交互
    Ajax中post与get的区别
    Web前端:2、盒模型的组成
    Web前端:1、HTML&CSS概述及结构
    VMware11虚拟机安装Redhat6.5视频演示
    VMware虚拟机中安装Linux系统步骤(Redhat6.5)
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5217432.html
Copyright © 2020-2023  润新知