标签:ble margin size_t border turn ica ext 最大 directly
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
Note:
num1 and num2 is < 110.num1 and num2 contains only digits 0-9.num1 and num2 does not contain any leading zero.Subscribe to see which companies asked this question.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | class Solution {public: string multiply(string num1, string num2) { string result; for(int i = num2.size() - 1; i >=0; --i){ string tmp = multi(num1, num2[i]-‘0‘); for(int j = num2.size() - i - 1; j > 0 && tmp !="0"; --j)tmp.push_back(‘0‘); result = add(result, tmp); } return result; } string multi(string s, int num){ if(num == 0) return "0"; int c = 0; string result; for(int i = s.size() - 1; i >= 0; --i){ int all = (s[i]-‘0‘) * num + c; result.push_back(all%10 + ‘0‘); c = all/10; } if(c != 0) result.push_back(‘0‘+c); reverse(result.begin(), result.end()); return result; } string add(string num1, string num2){ string result, s1, s2; s1.assign(num1.rbegin(), num1.rend()); s2.assign(num2.rbegin(), num2.rend()); int a, b, c = 0, sum = 0; auto l1 = s1.begin(), l2 = s2.begin(); while(l1 != s1.end() || l2 != s2.end()){ l1 != s1.end() ? a = (*l1) - ‘0‘: a = 0; l2 != s2.end() ? b = (*l2) - ‘0‘: b = 0; sum = a + b + c; result.push_back(sum%10 + ‘0‘); c = sum / 10; if(l1 != s1.end()) l1++; if(l2 != s2.end()) l2++; } if(c != 0){ result.push_back(c + ‘0‘); } reverse(result.begin(), result.end()); return result; }}; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Solution {public: string multiply(string num1, string num2) { int n = num1.size(), m = num2.size(); string result(n + m, ‘0‘); for(int i = n - 1; i >= 0; --i){ int c = 0; for(int j = m - 1; j >= 0; --j){ c = result[i + j + 1]-‘0‘ + (num1[i] - ‘0‘) * ( num2[j] - ‘0‘) + c; result[i + j + 1] = c % 10 + ‘0‘; c = c / 10; } //we can think that position j = -1, num2[j] == 0, then the result = result[i + j + 1] + c = result[i] + c; result[i] +=c; } size_t startpos = result.find_first_not_of("0"); if (string::npos != startpos) { return result.substr(startpos); } return "0"; } }; |
标签:ble margin size_t border turn ica ext 最大 directly
原文地址:http://www.cnblogs.com/zhxshseu/p/b0c7e123bf21e7ff24c27ca6e8aa08a9.html