Leetcode-Multiply Strings

LiBlog發表於2014-11-21

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.

Have you met this question in a real interview?
 
Solutiion:
a[i]*b[j] is the value of c[i+j]. So we have
 
Iter a[i]
  iter b[j]
      c[i+j] = a[i]*b[j]+c[i+j]
      carry = c[i+j]/10;
      c[i+j] = c[i+j]%10;
     c[i+j+1] += carry.  //NOTE: because we are dealing with string, we need to manipulate the char directly, after adding carry to c[i+j+1], c[i+j+1] may be larger than 10. Therefore, we need to keep dealing with carry, until in some higher bit, the carry becomes 0!
 
 Newest Solution:
Use int array, address all carry together afte calculation.
public class Solution {
    public String multiply(String num1, String num2) {
        if (num1.isEmpty() || num2.isEmpty()) return "";
        if (num1.equals("0") || num2.equals("0")) return "0";
        
        int len1 = num1.length(), len2 = num2.length();
        int[] res = new int[len1+len2];
        int[] n1 = new int[len1];
        int[] n2 = new int[len2];
        
        for (int i=0;i<len1;i++){
            n1[i] = getValue(num1.charAt(len1-1-i));
        }
        
        for (int i=0;i<len2;i++){
            n2[i] = getValue(num2.charAt(len2-1-i));
        }
        
        
        for (int j=0;j<len2;j++)
            for (int i=0;i<len1;i++){
                res[i+j] += n1[i]*n2[j];
            }
        
        StringBuilder builder = new StringBuilder();    
        for (int i=0;i<len1+len2-1;i++){
            res[i+1] += res[i] / 10;
            res[i] = res[i] % 10;
            builder.append(res[i]);
        }
        if (res[len1+len2-1]!=0)
            builder.append(res[len1+len2-1]);
        
        
        
        return builder.reverse().toString();
    }
    
    private static int getValue(char a){
        return (int) (a-'0');
    }
    
}

 

 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         if (num1.isEmpty() || num2.isEmpty()) return "";
 4         if (num1.equals("0") || num2.equals("0")) return "0";
 5         if (num1.equals("1")) return num2;
 6         if (num2.equals("1")) return num1;
 7 
 8 
 9         StringBuilder b1 = new StringBuilder(num1);
10         StringBuilder b2 = new StringBuilder(num2);
11         b1.reverse();
12         b2.reverse();
13         StringBuilder b3 = new StringBuilder();
14 
15         //perform multiplication bit by bit;
16         for (int i=0;i<b1.length();i++){
17             char c1 = b1.charAt(i);
18             for (int j=0;j<b2.length();j++){
19                 char c2 = b2.charAt(j);
20                 int val = (c1-'0')*(c2-'0');
21                 if (b3.length()>(i+j)) val += b3.charAt(i+j)-'0';
22                 int carry = val/10;
23                 val = val % 10;
24                 //put val of current bit into string.
25                 if (b3.length()>(i+j))
26                     b3.replace(i+j,i+j+1,Integer.toString(val));
27                 else b3.append((char)(val+'0'));
28 
29                 //put carry into (i+j+1) bit.
30                 if (carry==0) continue;
31                 int curBit = i+j+1;
32                 while (carry!=0){ 
33                     if (b3.length()>curBit){
34                         val = (b3.charAt(curBit)-'0')+carry;
35                         carry = val / 10;
36                         val = val%10;
37                         b3.replace(curBit,curBit+1,Integer.toString(val));
38                         curBit++;
39                     } else {
40                         b3.append((char)(carry+'0'));
41                         carry = 0;
42                     }
43                 }
44             }
45         }
46 
47         b3.reverse();
48 
49         return b3.toString(); 
50     }
51 }

 Solution2:

Directly use char arry. It is faster than StringBuilder.

 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         if (num1.isEmpty() || num2.isEmpty()) return "";
 4         if (num1.equals("0") || num2.equals("0")) return "0";
 5         if (num1.equals("1")) return num2;
 6         if (num2.equals("1")) return num1;
 7 
 8 
 9         StringBuilder temp = new StringBuilder(num1);
10         temp.reverse();
11         char[] b1 = temp.toString().toCharArray();
12         temp = new StringBuilder(num2);
13         temp.reverse();
14         char[] b2 = temp.toString().toCharArray();
15         char[] b3 = new char[b1.length+b2.length];
16         Arrays.fill(b3,'0');
17 
18 
19         //perform multiplication bit by bit;
20         for (int i=0;i<b1.length;i++){
21             char c1 = b1[i];
22             for (int j=0;j<b2.length;j++){
23                 char c2 = b2[j];
24                 int val = (c1-'0')*(c2-'0') + (b3[i+j]-'0');
25                 int carry = val/10;
26                 val = val % 10;
27                 //put val of current bit into string.
28                 b3[i+j] = (char) (val+'0');
29 
30                 //put carry into (i+j+1) bit.
31                 if (carry==0) continue;
32                 int curBit = i+j+1;
33                 while (carry!=0){ 
34                     val = (b3[curBit]-'0')+carry;
35                     carry = val / 10;
36                     val = val%10;
37                     b3[curBit] = (char)(val+'0'); 
38                     curBit++;
39                 }
40             }
41         }
42        
43         temp = new StringBuilder();
44         int index = b3.length-1;
45         for (int i=b3.length-1;i>=0;i--) 
46             if (b3[i]!='0'){
47                 index = i;
48                 break;
49             }
50         for (int i=index;i>=0;i--) temp.append(b3[i]);
51         
52 
53         return temp.toString(); 
54     }
55 }

 

 
 

相關文章