Problem:
Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
Solution:
Multiply each bit of string 1 with string2. Be careful of the carryin and the position.
Code:
public class Solution {public String multiply (string num1, String num2) {if (NUM1 = null | | num2 = NULL) RE Turn ""; Int[] result = new Int[num1.length () + num2.length ()]; int carryin = 0; for (int i = Num1.length ()-1; I >= 0; i--) {carryin = 0; for (int j = num2.length ()-1; J >= 0; j--) {int a = Num1.charat (i)-' 0 '; int B = Num2.charat (j)-' 0 '; int TMP = A * b + carryin + result[num1.length ()-1 + num2.length ()-1-i-J]; Carryin = TMP/10; Result[num1.length ()-1 + num2.length ()-1-i-j] = tmp% 10; } result[num1.length ()-1 + num2.length ()-1-i + 1] = Carryin; }//Result[num1.length () + num2.length ()-1] = Carryin; int i = num1.length () + num2.length ()-1; while (I >= 1 && result[i] = = 0) {i--; } String s = ""; for (int j = i; J >= 0; j--) {s = s + result[j]; } return s; }}
Leetcode:multiply Strings