Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
Test instructions: Multiplication of strings.
Idea: Simulate the idea of vertical multiplication, like the large number of calculations, the first string inverted
Class Solution {public:string Multiply (string num1, String num2) {reverse (Num1.begin (), Num1.end ()); Reverse (Num2.begin (), Num2.end ()); int len1 = Num1.size (), len2 = Num2.size (); String ans (len1+len2+1, ' 0 '); int tmp = 0; for (int i = 0; i < len1; i++) {int cur = num1[i]-' 0 '; TMP = 0; for (int j = 0; J < Len2; J + +) {int a = tmp + cur * (num2[j]-' 0 ') + (ans[i+j]-' 0 '); TMP = A/10; ANS[I+J] = a% 10 + ' 0 '; } int idx = LEN2; while (tmp! = 0) {int a = tmp + (ans[i+idx]-' 0 '); TMP = A/10; ANS[I+IDX] = a% 10 + ' 0 '; idx++; }} while (!ans.empty () && ans.back () = = ' 0 ') ans.pop_back (); if (Ans.empty ()) return "0"; Reverse (Ans.begin (), Ans.end ()); return ans; }};
Leetcode Multiply Strings