Run Time error: Working on my own Mac, unknown reason. Using recursion instead of PC.


  • 0
    Y

    Hi, I want to use recursion to solve this problem and below is my codes, I modified the parameter from string s into string &s.

    So I put the working code part inside the private section of the class. But the interpreter response me with the run time error. I tested it on my computer using clang as the compiler and it was working alright. Is there any one who can help me with this?

    Can I modified the parameter from copy the string into the reference?

    class Solution {
        
    public:
        bool isValid(string s){
            return isvalid(s);
        }
        
    private:
        bool isvalid(string &s) {
            if( s.length() == sizeof(char)){
                return false;
            }
            const char ele = pop_str(s);
            if(match(ele,s.back())){
                s.pop_back();
                return true;
            }
            else{
                bool con1 = isvalid(s);
                bool con2 = match(ele,pop_str(s));
                return (con1 && con2);
            }
        }
        char pop_str(string &s){
            char temp = s.back();
            s.pop_back();
            return temp;
        }
        bool match(char l1, char l2){
            if((l1=='[' && l2==']') ||(l2=='[' && l1==']') ){
                return true;
            }
            else if((l1 == '{' && l2 == '}') || (l2 == '{' && l1 == '}')){
                return true;
            }
            else if((l1 == '(' && l2 == ')')||( l2 == '(' && l1 == ')')){
                return true;
            }
            else{
                return false;
            }
        }
    };
    

    The Wrong answer

    But it is run time error online.

    It works on my computer

    By the way, have we included all the STL library by this OJ system?


Log in to reply
 

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