678. Valid Parenthesis String(自增運算符)

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 parenthesis ')'.
Any right parenthesis ')' must have a corresponding left parenthesis '('.
Left parenthesis '(' must go before the corresponding right parenthesis ')'.
'
' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
An empty string is also valid.
Example 1:
Input: "()"
Output: True
Example 2:
Input: "(*)"
Output: True

backtracking

之前這題有不帶*的情形,當時是用stack做的。那有了星號,我不知道怎么處理星號了。也想到能把三種情況都試一下,但是沒想到怎么搞,就算這么做了,如果*太多,分叉的情況就會乘3再成3,復雜度很高吧,就沒想下去了。看到別人用這個思路做了,回溯做的,沒有超時。
我試著模仿了一下,遇到了很多問題:

1. 寫成return dfs(s, count ++, start ++);
這么做IDE會提示你count, start的值沒有被用到,我很疑惑。后來想通了,因為++在后面的時候,是執行完了當前語句再自增的,所以進入下一層循環回來之后再增加已經沒卵用了。

2. 寫成return dfs(s, ++count , ++start);
跟1不同的是,這么做雖然改變了count和start的值,但是這么做無異于先count ++再把count當作參數傳入下一層遞歸,問題是這么做就沒有恢復現場的-- 操作了。所以,只能count + 1這么做。遞歸的參數要慎用自增運算符。

    public boolean checkValidString(String s) {
        if (s == null || s.length() == 0) return true;
        return dfs(s, 0, 0);
    }

    private boolean dfs(String s, int count, int start) {
        if (count < 0)
            return false;
        if (start >= s.length())
            return count == 0;
        if (s.charAt(start) == '(') {
            return dfs(s, count + 1, start + 1);
        }
        if (s.charAt(start) == ')') {
            return dfs(s, count - 1, start +1);
        }
        if (s.charAt(start) == '*') {
                //一旦遇到true就結束遞歸,一層層返回true
            return dfs(s, count + 1, start + 1)
                    || dfs(s, count - 1, start + 1)
                    || dfs(s, count, start + 1);
        }
        return count == 0;
    }

或寫成:

class Solution {
    public boolean checkValidString(String s) {
        return check(s, 0, 0);
    }
    
    private boolean check(String s, int start, int count) {
        if (count < 0) return false;
        
        for (int i = start; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                count++;
            }
            else if (c == ')') {
                if (count <= 0) return false;
                count--;
            }
            else if (c == '*') {
                return check(s, i + 1, count + 1) || check(s, i + 1, count - 1) || check(s, i + 1, count);
            }
        }
        
        return count == 0;
    }
}

One pass

看到一種O(n)的one pass解法,

    public boolean checkValidString(String s) {
        int low = 0;
        int high = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                low++;
                high++;
            } else if (s.charAt(i) == ')') {
                if (low > 0) {
                    low--;
                }
                high--;
            } else {
                if (low > 0) {
                    low--;
                }
                high++;
            }
            if (high < 0) {
                return false;
            }
        }
        return low == 0;
    }

當遇到*的時候,low要--同時high要++;
high is tracking maximum number of open braces possible '('.
if it encounters a *, it considers it as '('
low is tracking minimum number of open braces.
If it encounters a *, it considers it as ')'
In the end, if high is negative, that means there were too many ')'
If low < 0, it means there are more ')' than '(', which is invalid
https://discuss.leetcode.com/topic/103936/short-java-o-n-time-o-1-space-one-pass/6
不太懂,以后有機會再看吧

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容