Why the result is "Runtime Error" and "Last executed input: "0" " ,however,in my computer,it outputs 0.


  • 1
    T
        class Solution {
    public:
          int numDecodings(string s) {
                    int * num = new int[s.size() + 1];
                    memset(num,0,s.size() + 1);
    
                    int i = s.size() - 1;
                    if (i == 0)
                    {
                            if(s[i] != '0')
                                    return 1;
                            return 0;
                    }
    
                    num[i] = (s[i] == '0') ? 0 : 1;
                    if (i < 0)
                            return 0;
                    i --;
                    num[s.size()] = 1;
                    while (i >= 0)
                    {
                            int temp = (s[i] - '0') * 10 + s[i + 1] - '0';
                            if (temp == 0)
                                    return 0;
                            else if (temp <= 26 && temp > 0)
                                    num[i] = num[i + 1] + num[i + 2];
                            else
                                    num[i] = max(num[i + 1],1);
                    i --;
                    }
                    return num[0];
            }
    
    };

Log in to reply
 

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