zoukankan      html  css  js  c++  java
  • 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();
        }
    }

  • 相关阅读:
    ORA12560: TNS: 协议适配器错误的问题
    ibatis代码生成工具abator使用全过程
    DbHelper数据操作类
    眼睛有干涩、血丝、怕光,流泪,甚至红肿的现象吗
    Dot.Net代码生成器
    两分钟让你明白什么是ERP
    spring的b/s项目中配置log4j
    十面埋妇
    程序员发展的目标
    标准体重计算查询
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5217432.html
Copyright © 2011-2022 走看看