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

之前這題有不帶*的情形逸邦,當(dāng)時是用stack做的硼端。那有了星號从祝,我不知道怎么處理星號了。也想到能把三種情況都試一下,但是沒想到怎么搞,就算這么做了,如果*太多穗泵,分叉的情況就會乘3再成3,復(fù)雜度很高吧谜疤,就沒想下去了佃延∠志鳎看到別人用這個思路做了,回溯做的履肃,沒有超時仔沿。
我試著模仿了一下,遇到了很多問題:

1. 寫成return dfs(s, count ++, start ++);
這么做IDE會提示你count, start的值沒有被用到尺棋,我很疑惑封锉。后來想通了,因為++在后面的時候膘螟,是執(zhí)行完了當(dāng)前語句再自增的成福,所以進(jìn)入下一層循環(huán)回來之后再增加已經(jīng)沒卵用了。

2. 寫成return dfs(s, ++count , ++start);
跟1不同的是荆残,這么做雖然改變了count和start的值奴艾,但是這么做無異于先count ++再把count當(dāng)作參數(shù)傳入下一層遞歸,問題是這么做就沒有恢復(fù)現(xiàn)場的-- 操作了内斯。所以蕴潦,只能count + 1這么做。遞歸的參數(shù)要慎用自增運算符俘闯。

    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就結(jié)束遞歸品擎,一層層返回true
            return dfs(s, count + 1, start + 1)
                    || dfs(s, count - 1, start + 1)
                    || dfs(s, count, start + 1);
        }
        return count == 0;
    }

或?qū)懗桑?/p>

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;
    }

當(dāng)遇到*的時候备徐,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
不太懂甚颂,以后有機(jī)會再看吧

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蜜猾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子振诬,更是在濱河造成了極大的恐慌蹭睡,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赶么,死亡現(xiàn)場離奇詭異肩豁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)辫呻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門清钥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人放闺,你說我怎么就攤上這事祟昭。” “怎么了怖侦?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵篡悟,是天一觀的道長谜叹。 經(jīng)常有香客問我,道長搬葬,這世上最難降的妖魔是什么荷腊? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮急凰,結(jié)果婚禮上女仰,老公的妹妹穿的比我還像新娘。我一直安慰自己香府,他們只是感情好董栽,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著企孩,像睡著了一般锭碳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勿璃,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天擒抛,我揣著相機(jī)與錄音,去河邊找鬼补疑。 笑死歧沪,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的莲组。 我是一名探鬼主播诊胞,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼锹杈!你這毒婦竟也來了撵孤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤竭望,失蹤者是張志新(化名)和其女友劉穎邪码,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體咬清,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡闭专,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了旧烧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片影钉。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖粪滤,靈堂內(nèi)的尸體忽然破棺而出斧拍,到底是詐尸還是另有隱情,我是刑警寧澤杖小,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布肆汹,位于F島的核電站愚墓,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏昂勉。R本人自食惡果不足惜浪册,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望岗照。 院中可真熱鬧村象,春花似錦、人聲如沸攒至。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽迫吐。三九已至库菲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間志膀,已是汗流浹背熙宇。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留溉浙,地道東北人烫止。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像戳稽,于是被迫代替她去往敵國和親馆蠕。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容