[LeetCode]Longest Substring Without Repeating Characters

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note: The numbers can be arbitrarily large and are non-negative.

大整数乘法,通过模拟手算过程来完成。

把结果存在整型数组里,先做乘加再考虑进位问题。

/*Inspired by Manual Multiply calculation process*/
class Solution {
public:
    string multiply(string num1, string num2) {
        int len1 = num1.size();
        int len2 = num2.size();
        if(num1=="0"||num2=="0")
            return "0";
        vector<int> res(len1+len2,0); 
        
        for(int i=0; i<len1; ++i)
            for(int j=0; j<len2; ++j)
                res[i+j+1] += (num1[i]-'0')*(num2[j]-'0'); //multiply but not carry
                
        string s = "";
        for(int k = len1+len2-1; k>=0; --k){ //vector int change to string
            if(k>0)
                res[k-1] += res[k]/10;
            res[k] = res[k]%10;
            char temp = '0'+res[k];
            s = temp+s;
        }
        return s[0]=='0'?s.substr(1):s;
    }
};


你可能感兴趣的:([LeetCode]Longest Substring Without Repeating Characters)