http://www.cnblogs.com/TenosDoIt/p/3735309.html
https://blog.csdn.net/fly_yr/article/details/48055617
class Solution { public: string multiply(string num1, string num2) { int len1 = num1.length(); int len2 = num2.length(); vector<int> res(len1+len2,0); int k = len1 + len2 - 2; 第一轮乘出来的结果只有len1+len2-1个,数组的index为0再减1 for(int i = 0;i < len1;i++){ for(int j = 0;j < len2;j++){ res[k - i - j] += (num1[i] - ‘0‘) * (num2[j] - ‘0‘); num1[0]是原本数的最高位,这里做了个转换,把最低位的数放在了0位置 } } int carry = 0; for(int i = 0;i < len1 + len2;i++){ res[i] += carry; carry = res[i]/10; res[i] = res[i]%10; } int i = k + 1; while(res[i] == 0) 找寻第一个非0的位置 i--; if(i < 0) return "0"; string result; for(;i >= 0;i--) result.push_back(res[i] + ‘0‘); return result; } };
原文地址:https://www.cnblogs.com/ymjyqsx/p/9464311.html
时间: 2024-10-07 14:31:31