Title Description:
Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
Problem Solving Ideas:
Sets the result that the array records the multiplication of a single position, and finally the additive rounding.
The code is as follows:
public class Solution {public String multiply (string num1, String num2) {int num1length = num1.length (); int Num2lengt h = num2.length (); int D1, D2;int carry = 0;int temp;int[] caculate = new Int[num1length + num2length]; StringBuilder sb = new StringBuilder (), for (int i = Num1.length ()-1, i >= 0; i--) {D1 = Num1.charat (i)-' 0 '; for (int j = Num2.length ()-1; J >= 0; j--) {D2 = Num2.charat (j)-' 0 '; Caculate[i + j + 1] + = D1 * D2;} for (int i = caculate.length-1; I >= 0; i--) {temp = (Caculate[i] + carry)% 10;carry = (Caculate[i] + carry)/10;ca Culate[i] = temp;} for (int num:caculate) sb.append (num), while (Sb.length () > 1 && sb.charat (0) = = ' 0 ') {sb.deletecharat (0);} return sb.tostring ();}}
Java [Leetcode 43]multiply Strings