Friday, February 28, 2014

Leecode: Multiply String


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.
大数相乘的算法
  • 直接会cause overflow,所以每次都要两个single digit相乘,最大81,不会溢出。
  • 比如385 * 97, 就是个位=5 * 7,十位=8 * 7 + 5 * 9 ,百位=3 * 7 + 8 * 9,  千位=3*9
    可以每一位用一个Int表示,存在一个int[]里面。
  • 这个数组最大长度是num1.len + num2.len,比如99 * 99,最大不会超过10000,所以4位就够了。
  • 这种个位在后面的,不好做(10的0次方,可惜对应位的数组index不是0而是n-1),
    所以干脆先把string reverse了代码就清晰好多。
  • 最后结果前面的0要清掉。
Code:

public String multiply(String num1, String num2) {
    num1 = new StringBuilder(num1).reverse().toString();
    num2 = new StringBuilder(num2).reverse().toString();
    // even 99 * 99 is < 10000, so maximaly 4 digits
    int[] d = new int[num1.length() + num2.length()];
    for (int i = 0; i < num1.length(); i++) {
        int a = num1.charAt(i) - '0';
        for (int j = 0; j < num2.length(); j++) {
            int b = num2.charAt(j) - '0';
            d[i + j] += a * b;
        }
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < d.length; i++) {
        int digit = d[i] % 10;
        int carry = d[i] / 10;
        sb.insert(0, digit);
        if (i < d.length - 1)
            d[i + 1] += carry;
        }
    //trim starting zeros
    while (sb.length() > 0 && sb.charAt(0) == '0') {
        sb.deleteCharAt(0);
    }
    return sb.length() == 0 ? "0" : sb.toString();
}

完全按照两个数相乘的步骤做法:

public String multiply(String num1, String num2) {
        String ret = "";
        
        if(num1.length()==0 || num2.length()==0) return ret;
        if(num1.equals("0") || num2.equals("0")) return "0";
        
        int[] data = new int[num1.length() + num2.length()];
        
        for(int i = num1.length()-1; i>=0; i--){
            int a = num1.charAt(i) - '0';
            for(int j = num2.length()-1; j>=0; j--){
                int b = num2.charAt(j)- '0';
                data[i+j+1] += a*b;
            }
        }
        
        int digit = 0;
        int carry = 0;
         
        StringBuilder rt = new StringBuilder();
        for(int i= data.length-1; i>=0; i--){
            digit = (data[i]+carry)%10;
            rt.insert(0, digit);
            carry = (data[i]+carry)/10;
        }
        
        for(int i =0; i< rt.length(); i++){
            if(rt.charAt(0) == '0')
                rt.deleteCharAt(0);
        }
        
        ret = rt.length()==0? "0" : rt.toString();
        
        return ret;
    }



No comments:

Post a Comment