Why same code yield different result between OJ and Visual Studio for same test case?


  • -1
    H

    my code return true in Visual Studio, yet OJ return false.
    test case: char* s = "[{()}]";

    my code:
    struct stack
    {
    char c;
    struct stack* next;
    struct stack* pre;
    };
    bool isValid(char* s)
    {
    if (s == '\0') return true;
    if (
    (s + 1) == '\0') return false;
    struct stack* sk;
    struct stack headnode;
    headnode.c = '0';
    struct stack firstnode;
    firstnode.c = *s;
    sk = &firstnode;
    sk->pre = &headnode;
    headnode.next = sk;
    s++;
    while (*s != '\0')
    {
    switch (*s)
    {
    case '(':
    {
    struct stack node;
    node.c = '(';
    sk->next = &node;
    node.pre = sk;
    sk = sk->next;
    break;
    }
    case ')':
    {
    if (sk->c == '(')
    sk = sk->pre;
    else
    return false;
    break;
    }

    		case '[':
    		{
    					struct stack node;
    					node.c = '[';
    					sk->next = &node;
    					node.pre = sk;
    					sk = sk->next;
    					break;
    		}
    		case ']':
    		{
    					if (sk->c == '[')
    						sk = sk->pre;
    					else
    						return false;
    					break;
    		}
    		case '{':
    		{
    					struct stack node;
    					node.c = '{';
    					sk->next = &node;
    					node.pre = sk;
    					sk = sk->next;
    					break;
    		}
    		case '}':
    		{
    					if (sk->c == '{')
    						sk = sk->pre;
    					else
    						return false;
    					break;
    		}			
    	}
    	s++;
    }
    return sk == &headnode;
    

    }


Log in to reply
 

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