0ms solution using array instead of stack


  • 0
    S
    bool isValid(char* s) {
      int i;
      int len = strlen (s);
      if(len % 2)  return false;
      len = len / 2 + 1;
      char array[len];
      for (i = 0; i < len; i++)
        array[i] = '0';
      i = 0;
      while (s != NULL && *s && i < len)
      {
        switch(*s)
        {
          case '(' :
          case '{' :
          case '[' : array[i++] = *s;
                     break;
          case ')' : if (array[--i] != '(')  return false;
                     break;
          case '}' : if (array[--i] != '{')  return false;
                     break;
          case ']' : if (array[--i] != '[')  return false;
                     break;
          default : return false;
        }
        s++;
      }
      return !i;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.