Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
---
09/21, Review
顺序,
最高位是0要去掉
public class Solution { public String multiply(String num1, String num2) { if (num1.equals("0") || num2.equals("0")) return "0"; int l1 = num1.length(); int l2 = num2.length(); int newlen = l1+l2; int[] n1 = new int[l1]; int[] n2 = new int[l2]; int[] res = new int[newlen]; // convert string to number array for (int i=0; i0 && i
---
Reference
public class Solution { public String multiply(String num1, String num2) { if (num1.equals("0") || num2.equals("0")) return "0"; int l1 = num1.length(), l2 = num2.length(); int newlen = l1+l2; int[] n1 = new int[l1]; int[] n2 = new int[l2]; int[] res = new int[newlen]; // convert string to number array for (int i=0; i=0; k--) { sb.append((char)(res[k] % 10 + '0')); if (k>0) res[k-1] += res[k] / 10; } // trim 0 at the biginning if(sb.charAt(newlen- 1) == '0') sb.deleteCharAt(newlen- 1); // reverse sb = sb.reverse(); return sb.toString(); }}