1. 程式人生 > >43. Multiply Strings

43. Multiply Strings

col length stringbu ply div style uil build new

public class Solution {
    public String multiply(String num1, String num2) {
        int[] digits=new int[num1.length()+num2.length()];
        for(int i=0;i<num1.length();i++)
            for(int j=0;j<num2.length();j++)
                digits[digits.length-i-j-1]+=(num1.charAt(num1.length()-i-1)-‘0‘)*(num2.charAt(num2.length()-j-1)-‘0‘);
        StringBuilder sb
=new StringBuilder(); for(int i=digits.length-1;i>=0;i--) { if(i>0) digits[i-1]+=digits[i]/10; sb.insert(0,(char)(‘0‘+digits[i]%10)); } int idx=0; while(idx<sb.length()-1&&sb.charAt(idx)==‘0‘) idx
++; return sb.substring(idx).toString(); } }

43. Multiply Strings