topic:
Includes only a given '(', ')' string, '{', '}', '[', ']', and determine whether the string is valid.
Valid string must meet:
- Left bracket must be closed by a closing parenthesis of the same type.
- Left parenthesis must be closed in the correct order.
Note the empty string can be considered a valid string.
Example 1
输入: "()"
输出: true
Example 2
输入: "()[]{}"
输出: true
Example 3
输入: "(]"
输出: false
Example 4
输入: "([)]"
输出: false
Example 5
输入: "{[]}"
输出: true
Algorithm
class Solution {
public:
bool isValid(string s) {
int historyS[10000];
int temp;
int j = 0;
unordered_map<char, int> Symbol{ {'{', 1}, { '}', -1}, {'[', 100}, {']', -100}, {'(', 1000}, {')', -1000} };
if (Symbol[s[0]] < 0 )
return 0;
if (s.length() == 0)
return 1;
else
historyS[0] = Symbol[s[0]];
for (int i = 1; i < s.length(); i++) {
int temp = Symbol[s[i]];
if (temp < 0) {
if (j <= -1 || temp + historyS[j] != 0 )
return 0;
j--;
}
else {
historyS[j + 1] = temp;
j++;
}
}
return j == -1 ? 1 : 0;
}
};
result:
The first one hundred double