Why run time error with "1","1" input , however I can run it on my macbook


  • 0
    D

    class Solution {
    public:
    string multiply(string num1, string num2) {
    int i, j;
    string ret(num1.size()+num2.size(),'0');
    long rem;

    reverse(num1.begin(),num2.end());
    reverse(num2.begin(),num2.end());
    
    long carry;
    for(i=0;i<num2.size();i++)
        {   
        carry=0;
        for(j=0;j<num1.size();j++)
            {   
            rem=((num1[j]-'0')*(num2[i]-'0')+carry+(ret[i+j]-'0'))%10;
            carry=((num1[j]-'0')*(num2[i]-'0')+carry+(ret[i+j]-'0'))/10;
            ret[i+j]=rem+'0';;
            }   
        }   
    if(ret[ret.size()-1]=='0')
        ret.pop_back();
    reverse(ret.begin(),ret.end());
    return ret;
    }   
    

    };


  • 0
    D

    modified: (pass)

    class Solution {
    public:
    string multiply(string num1, string num2) {
    int i, j;
    string ret(num1.size()+num2.size(),'0');
    long rem;

    reverse(num1.begin(),num1.end());
    reverse(num2.begin(),num2.end());
    
    long carry;
    for(i=0;i<num2.size();i++)
        {   
        carry=0;
        for(j=0;j<num1.size();j++)
            {   
            rem=((num1[j]-'0')*(num2[i]-'0')+carry+(ret[i+j]-'0'))%10;
            carry=((num1[j]-'0')*(num2[i]-'0')+carry+(ret[i+j]-'0'))/10;
            ret[i+j]=rem+'0';;
            }
            if(carry>0)
                ret[i+j]=carry+'0';
        }  
    
    for(i=ret.size()-1;ret[i]=='0'&&i>=1;i--)
        ret.pop_back();
    reverse(ret.begin(),ret.end());
    return ret;
    }   
    

    };


Log in to reply
 

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