zoukankan      html  css  js  c++  java
  • leetcode678- Valid Parenthesis String- medium

    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:

    1. Any left parenthesis '(' must have a corresponding right parenthesis ')'.
    2. Any right parenthesis ')' must have a corresponding left parenthesis '('.
    3. Left parenthesis '(' must go before the corresponding right parenthesis ')'.
    4. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
    5. An empty string is also valid.

     Example 1:

    Input: "()"
    Output: True
    

     Example 2:

    Input: "(*)"
    Output: True
    

     Example 3:

    Input: "(*))"
    Output: True

    Note:

    1. The string size will be in the range [1, 100].

    1.O(n) 空间。用两个Stack<Integer>分别记录left和star出现的下标。导致失败的情况是:1.出现right的时候,left和star合起来都不够用了。2.遍历完以后left和star配对消不掉:表现为,栈顶的star下标比栈顶的left下标小 就好像*( ,或者star先被用完了(用最后返回left.isEmpty()来表现)。

    2.O(1)空间。用两个int most, least记录着现在还有多少个left可用,区别是most记录把星号当left用的情况,least记录把星号当right用的情况。导致失败的情况是:1.出现right的时候,most都到0了。 2.遍历完以后,least还>0,说明最后星号不够抵消。  更新情况是这样的:1.出现left,most, least都++。2.出现right,most--, least没到0的话-- 3.出现star,most++, least没到0的话--。(23里都对least很宽容的原因是,用most到0就失败的机制,已经避免了right出现太多的情况,那这个时候出现right时least已经到0只能是因为中间出现过star帮忙消了left造成的,这时候你就不用多减了,当之前的star是空气,现在的right实打实地替代了前面那个*即可)

    实现1:

    class Solution {
        public boolean checkValidString(String s) {
            Stack<Integer> left = new Stack<>();
            Stack<Integer> star = new Stack<>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '(') {
                    left.push(i);
                } else if (c == '*') {
                    star.push(i);
                } else {
                    if (!left.isEmpty()) {
                        left.pop();
                    } else if (!star.isEmpty()) {
                        star.pop();
                    } else {
                        return false;
                    }
                }
            }
            while (!left.isEmpty() && !star.isEmpty()) {
                int leftI = left.pop();
                int starI = star.pop();
                if (starI < leftI) {
                    return false;
                }
            }
            return left.isEmpty();
        }
    }
    

      

    实现2:

    class Solution {
        public boolean checkValidString(String s) {
            // number of '(' when treating '*' as '('
            int most = 0;
            // number of '(' when treating '*' as ')'
            int least = 0;
            
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '(') {
                    least++;
                    most++;
                } else if (c == ')') {
                    if (least > 0) {
                        least--;
                    }
                    most--;
                } else {
                    if (least > 0) {
                        least--;
                    }
                    most++;
                }
                if (most < 0) {
                    return false;
                }
            }
            return least == 0;
        }
    }
  • 相关阅读:
    Hashtable源码分析
    ConcurrentHashMap1.7源码分析
    JDK1.8新特性
    回炉Spring--Bean生命周期及AOP
    @DateTimeFormat 和 @JsonFormat 注解
    面向切面编程-日志切面应用及MDC使用
    maven 多模块开发
    maven 安装
    ztree树节点重叠问题
    Java问题解读系列之IO相关---Java深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7982476.html
Copyright © 2011-2022 走看看