【leetcode】Valid Parentheses

来源:互联网 发布:vue.js chrome插件 编辑:程序博客网 时间:2024/06/14 03:19

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.


bool isValid(char *s) {  const int size = 10000;  char stack[size];  int p = 0;  for (; *s != '\0'; ++s) {    switch (*s) {    case ')':      if (p == 0 || stack[p - 1] != '(') {        return false;      }      --p;      break;    case ']':      if (p == 0 || stack[p - 1] != '[') {        return false;      }      --p;      break;    case '}':      if (p == 0 || stack[p - 1] != '{') {        return false;      }      --p;      break;    default:      stack[p++] = *s;    } // switch  }  return p == 0;}


0 0
原创粉丝点击