[关闭]
@chawuciren 2018-11-21T13:13:54.000000Z 字数 850 阅读 563
  1. Valid Parentheses(没有AC)

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

  1. bool isValid(char* s) {
  2. int len;
  3. len=strlen(s);
  4. if(len==0)
  5. return true;
  6. char str[len+1];
  7. int top=0;
  8. for(int i=0;i<len+1;i++){
  9. switch(s[i]){
  10. case '(':str[++top]='(';
  11. break;
  12. case '[':str[++top]='[';
  13. break;
  14. case '{':str[++top]='{';
  15. break;
  16. case ')':if(str[top]=='(') --top;
  17. if(top!=0) return false;
  18. break;
  19. case ']':if(str[top]=='[') --top;
  20. if(top!=0) return false;
  21. break;
  22. case '}':if(str[top]=='{') --top;
  23. if(top!=0) return false;
  24. break;
  25. }
  26. }
  27. return true;
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注