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ī)會再看吧