首页 > 其他 > 详细

678. Valid Parenthesis String

时间:2018-01-14 19:57:20      阅读:230      评论:0      收藏:0      [点我收藏+]
/*
        一开始想的是双指针,从两边开始检测,如果有*或者匹配就前进,但是想了想不对,因为可能此时不匹配,但是前边会有*来配对,
        或者此时的*不知道应该当做什么,因为会对前边的有影响。
        由于*和(会对后边的有影响,所以要把坐标存起来
         */
        if (s.length()==0)
            return true;
        int l = s.length();
        Stack<Integer> le = new Stack<>();
        Stack<Integer> st = new Stack<>();

        for (int i = 0; i < l; i++) {
            char c = s.charAt(i);
            if (c==‘(‘)
                le.push(i);
            else if (c==‘*‘)
                st.push(i);
            else{
                if (!le.isEmpty())
                    le.pop();
                else if (st.isEmpty())
                    st.pop();
                else
                    return false;
            }

        }
        while (!le.isEmpty())
        {
            int i = le.pop();
            if (!st.isEmpty())
            {
                int j = st.pop();
                if (i > j)
                    return false;
            }
            else return false;

        };
        return true;

 

678. Valid Parenthesis String

原文:https://www.cnblogs.com/stAr-1/p/8284071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!