@chawuciren
2018-11-21T13:13:54.000000Z
字数 850
阅读 669
leetcode
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid(有效的).
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
bool isValid(char* s) {
int len;
len=strlen(s);
if(len==0)
return true;
char str[len+1];
int top=0;
for(int i=0;i<len+1;i++){
switch(s[i]){
case '(':str[++top]='(';
break;
case '[':str[++top]='[';
break;
case '{':str[++top]='{';
break;
case ')':if(str[top]=='(') --top;
if(top!=0) return false;
break;
case ']':if(str[top]=='[') --top;
if(top!=0) return false;
break;
case '}':if(str[top]=='{') --top;
if(top!=0) return false;
break;
}
}
return true;
}