[Leetcode] 678. Valid Parenthesis String 解题报告
2018-02-01 17:03
357 查看
题目:
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
Any right parenthesis
Left parenthesis
a single left parenthesis
An empty string is also valid.
Example 1:
Example 2:
Example 3:
Note:
The string size will be in the range [1, 100].
思路:
1、回溯法:我首先想到的是回溯法:每个'*'有三种可能,我们分别进行枚举。需要保证在任何时候,左括弧的数量都大于等于右括弧的数量,并且最终两者相等。请见下面的代码。虽然回溯法可以通过所有测试,但是一旦'*'的数量很多,则算法的时间复杂度就变得很高(假设有n个‘*’,则需要枚举3^n种可能性)。
2、范围法:这是在网上看到的更巧妙的一种方法:我们记录'('没有被')’match的范围[lower, upper]。任何时刻,lower都应该大于等于0,upper也应该大于等于0,并且最终lower需要等于0。需要注意的是,任何时刻如果lower小于0并不需要返回false,因为这种减少有可能是因为‘*’的出现而导致的,此时只需要修补一下,让lower大于等于0即可(意味着在某些地方'*'不能被当做‘)’,而只能当做'('来用);但是upper在任何时候都不能小于0,因为这意味着该时刻‘)’超过了‘(’和‘*’的总和。最后检查lower是否等于0即可。
代码:
1、回溯法:
class Solution {
public:
bool checkValidString(string s) {
return checkValidString(s, 0, 0, 0);
}
private:
bool checkValidString(string &s, int pos, int left_num, int right_num) {
if (left_num < right_num) {
return false;
}
if (pos == s.length()) {
return left_num == right_num;
}
if (s[pos] == '(') {
return checkValidString(s, pos + 1, left_num + 1, right_num);
}
else if (s[pos] == ')') {
return checkValidString(s, pos + 1, left_num, right_num + 1);
}
else {
return checkValidString(s, pos + 1, left_num, right_num + 1) ||
checkValidString(s, pos + 1, left_num, right_num) ||
checkValidString(s, pos + 1, left_num + 1, right_num);
}
}
};
2、范围法:
class Solution {
public:
bool checkValidString(string s) {
int lower = 0, upper = 0;
for (char c : s) {
if (c=='(') {
lower++;
upper++;
} else if (c==')') {
lower--;
upper--;
} else { // * encountered
lower--;
upper++;
}
lower = max(lower, 0); // lower <= 0 means we have unmatched ')'
if (upper < 0) { // unmatched ')' found in the middle of string
return false;
}
}
return lower == 0;
}
};
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
Example 3:
Input: "(*))" Output: True
Note:
The string size will be in the range [1, 100].
思路:
1、回溯法:我首先想到的是回溯法:每个'*'有三种可能,我们分别进行枚举。需要保证在任何时候,左括弧的数量都大于等于右括弧的数量,并且最终两者相等。请见下面的代码。虽然回溯法可以通过所有测试,但是一旦'*'的数量很多,则算法的时间复杂度就变得很高(假设有n个‘*’,则需要枚举3^n种可能性)。
2、范围法:这是在网上看到的更巧妙的一种方法:我们记录'('没有被')’match的范围[lower, upper]。任何时刻,lower都应该大于等于0,upper也应该大于等于0,并且最终lower需要等于0。需要注意的是,任何时刻如果lower小于0并不需要返回false,因为这种减少有可能是因为‘*’的出现而导致的,此时只需要修补一下,让lower大于等于0即可(意味着在某些地方'*'不能被当做‘)’,而只能当做'('来用);但是upper在任何时候都不能小于0,因为这意味着该时刻‘)’超过了‘(’和‘*’的总和。最后检查lower是否等于0即可。
代码:
1、回溯法:
class Solution {
public:
bool checkValidString(string s) {
return checkValidString(s, 0, 0, 0);
}
private:
bool checkValidString(string &s, int pos, int left_num, int right_num) {
if (left_num < right_num) {
return false;
}
if (pos == s.length()) {
return left_num == right_num;
}
if (s[pos] == '(') {
return checkValidString(s, pos + 1, left_num + 1, right_num);
}
else if (s[pos] == ')') {
return checkValidString(s, pos + 1, left_num, right_num + 1);
}
else {
return checkValidString(s, pos + 1, left_num, right_num + 1) ||
checkValidString(s, pos + 1, left_num, right_num) ||
checkValidString(s, pos + 1, left_num + 1, right_num);
}
}
};
2、范围法:
class Solution {
public:
bool checkValidString(string s) {
int lower = 0, upper = 0;
for (char c : s) {
if (c=='(') {
lower++;
upper++;
} else if (c==')') {
lower--;
upper--;
} else { // * encountered
lower--;
upper++;
}
lower = max(lower, 0); // lower <= 0 means we have unmatched ')'
if (upper < 0) { // unmatched ')' found in the middle of string
return false;
}
}
return lower == 0;
}
};
相关文章推荐
- LeetCode678. Valid Parenthesis String
- leetcode 678. Valid Parenthesis String 有效括号的判断 + 保存index遍历
- [LeetCode] 678. Valid Parenthesis String 验证括号字符串
- 【LeetCode】Substring with Concatenation of All Words 解题报告
- [leetcode] 79. Word Search 解题报告
- [Leetcode] 140. Word Break II 解题报告
- Leetcode 434. Number of Segments in a String 字符串的分段 解题报告
- [Leetcode] 732. My Calendar III 解题报告
- [leetcode] 143. Reorder List 解题报告
- [leetcode] 25. Reverse Nodes in k-Group 解题报告
- 【LeetCode解题报告】Add Digits
- LeetCode: Anagrams 解题报告
- [leetcode] 87. Scramble String 解题报告
- [LeetCode]Spiral Matrix,解题报告
- [Leetcode] 616. Add Bold Tag in String 解题报告
- leetcode解题报告3. Longest Substring Without Repeating Characters
- [Leetcode] 236. Lowest Common Ancestor of a Binary Tree 解题报告
- [LeetCode] Minimum Path Sum 解题报告
- 【leetcode】485.解题报告
- Leetcode 450. Delete Node in a BST 删除BST中的节点 解题报告