678. Valid Parenthesis String

/*
        一开始想的是双指针,从两边开始检测,如果有*或者匹配就前进,但是想了想不对,因为可能此时不匹配,但是前边会有*来配对,
        或者此时的*不知道应该当做什么,因为会对前边的有影响。
        由于*和(会对后边的有影响,所以要把坐标存起来
         */
        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;

 

posted @ 2018-01-14 19:45  stAr_1  阅读(316)  评论(0编辑  收藏  举报