标签:
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
1 public class Solution { 2 public String multiply(String num1, String num2) { 3 StringBuilder ans = new StringBuilder(); 4 int[] tmp = new int[num1.length() + num2.length()]; 5 for(int i = num1.length()-1; i>=0;i--){ 6 for(int j = num2.length()-1; j>=0;j--){ 7 tmp[i+j+1] += (num1.charAt(i) - ‘0‘)*(num2.charAt(j) - ‘0‘); 8 } 9 } 10 11 for(int i = tmp.length-1; i >= 1;i--){ 12 tmp[i-1] += tmp[i]/10; 13 tmp[i] %= 10; 14 } 15 16 int left = 0; 17 while(left < tmp.length-1 && tmp[left] == 0)left++;// 这里需要考虑一个 num1 和 num2 有一个为0 的情况。 18 for(;left < tmp.length; left++){ 19 ans.append(tmp[left]); 20 } 21 return ans.toString(); 22 } 23 }
43. Multiply Strings java solutions
标签:
原文地址:http://www.cnblogs.com/guoguolan/p/5624177.html