int[] ans = newint[num1.length() + num2.length()]; for (int i = num1.length() - 1; i >= 0; i--) for (int j = num2.length() - 1; j >= 0; j--){ int n1 = num1.charAt(i) - '0'; int n2 = num2.charAt(j) - '0'; if(n1 == 0 || n2 ==0) continue; int temp = n1 * n2 + ans[i+j+1]; ans[i+j+1] = temp % 10; ans[i+j] += temp / 10; }
StringBuilder result = new StringBuilder(); for (int i = 0; i < ans.length; i++) { //两个数相乘,位数是n+m(全9)或者n+m-1(只有一个1),因此只有i=0的时候,才有可能抛弃一位0 if (i == 0 && ans[i] == 0) continue; result.append(ans[i]); } return result.toString(); }