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;

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

时间: 2024-08-30 16:51:23

678. Valid Parenthesis String的相关文章

LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)

题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parent

leetcode_20 Valid Parentheses(String)

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 "([)]" 

SOAP-ERROR: Encoding: string … is not a valid utf-8 string

PHP 利用SOAP调用webservice 传输汉字数据时,开发环境使用GBK模式,报上述错误,解决方法,在配置中使用如下语句: $server = new SoapServer(null, array(    'uri' =>'http://soap/',    'encoding'=>'ISO-8859-1'    //'location'=>'http://61.160.96.114:8000/inc/service/serverSoap.php'    )); $soap =

字符串中的匹配之递归

字符串的括号匹配是一个很常见的问题.用栈这种后进先出的结构是非常适合的.此外,字符串中的回文以及衍生的各种问题也是字符串处理中非常常见的. 今天再说一下这类相似的问题,如何用递归来转化成子结构来求解. 先放一条LeetCode例题: 680. Valid Palindrome II Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

Longest Valid Parentheses leetcode java

题目: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf"; // Label1.Text = string.Format("asdfadsf{0:C}adsfas

.net String.Format数字格式化输出

内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充. int a = 12345678; //格式为sring输出 Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf&quo