“101”was test OK in my local IDE, but it notifes "Runtime error " why??


  • 1
    L

    class Solution {

    private:
    int* sub_result;
    public:

    int fun(string s, int begin ,int end){
        int sum = 0;
        if(begin == end){
            sub_result[begin] = 1;
            return 1;
        }
    
         if(sub_result[begin] != -1){
            return sub_result[begin];
        }
    
        if(s[begin] == '0'){
            sub_result[begin] = 0;
            return 0;
        }
    
        else{
            sum += fun(s,begin+1,end);
            //cout<<"sum = "<<sum<<endl;
            if(begin + 1 < end){
                int value = atoi(s.substr(begin,2).c_str());
                if(value <= 26){
                    sum += fun(s,begin+2,end);
                }
            }
    
    
        sub_result[begin] = sum;
        return sum;
        }
    }
    
    int numDecodings(string s) {
        if(s.length() == 0){
            return 0;
        }
    
        sub_result = new int[s.length()];
        for(int i = 0;i<s.length();i++){
            sub_result[i] = -1;
        }
    
    
        int result = fun(s,0,s.length());
    
        delete sub_result;
    
        return result;
    
    }
    

    };

    int main(){

    Solution s;
    cout<<s.numDecodings("101")<<endl;
    //cout<<s.numDecodings("120")<<endl;
    //cout<<s.numDecodings("1002")<<endl;
    return 0;
    

    }


Log in to reply
 

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