43. Multiply Strings Answer
Anonymous User
118
class Solution {
public:
    string multiply(string num1, string num2) {
        long long ans=0, x;
        int i, j;
        int num1_sz = num1.size()-1, num2_sz = num2.size()-1;
        
        for (i=num1_sz; i>-1; i--) {
            x = pow(10, num1_sz-i)*(num1[i]-'0');
            for (j=num2_sz; j>-1; j--) {
                ans += x*pow(10, num2_sz-j)*(num2[j]-'0');
            }
        }
        return to_string(ans);
    }
};

Can someone tell me why this code doesn't work?

It produces an error under certian input such as

input "123456789" "987654321"
output "121932631112635264"
answer "121932631112635269"

and Is there an algorithm that's under O(n^2) ?

Thanks.

Comments (1)