Multiply Strings

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.

 

 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         String large = null;
 6         String small = null;
 7         if(num1.length() > num2.length()){
 8             large = num1;
 9             small = num2;
10         }else{
11             large = num2;
12             small = num1;
13         }
14         if(num1.equals("0") || num2.equals("0")){
15             return "0";
16         }
17         StringBuffer result = new StringBuffer();
18         int carry = 0;
19         for(int l = large.length() + small.length() - 2; l > -1; l --){
20             int sum = carry;
21             for(int j = small.length() - 1; j > -1; j --){
22                 int i = l - j;
23                 if(i < large.length() && i > -1){
24                     sum += (large.charAt(i) - '0') * (small.charAt(j) - '0');
25                 }
26             }
27             carry = sum / 10;
28             result.insert(0,sum % 10);
29         }
30         if(carry != 0){
31             result.insert(0,carry);
32         }
33         return result.toString();
34     }   
35 }

 第二遍:

利用array 可以简化运算!

 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         if ( num1.equals("0") || num2.equals("0") ) return "0";
 6         int n1=num1.length(), n2 = num2.length();
 7         int[] c = new int[n1 + n2];
 8         for (int i = n1 - 1; i > -1; i--){
 9             for (int j = n2 - 1; j > -1; j--){
10                 c[i + j + 1] += (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
11             }
12         }
13         for (int o=n1 + n2 - 1; o > 0; o --){
14             c[o - 1] += c[o]/10;
15             c[o] %= 10;
16         }
17         int bit = 0;
18         if(c[0] == 0) bit = 1;
19         StringBuffer ret = new StringBuffer();
20         for (int i=bit; i < n1 + n2; i ++)
21             ret.append(c[i]);
22         return ret.toString();
23     }   
24 }

 第三遍:

 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         if(num1.equals("0") || num2.equals("0")) return "0";
 4         int len1 = num1.length(), len2 = num2.length();
 5         int[] result = new int[len1 + len2 - 1];
 6         for(int i = 0; i < len1; i ++){
 7             for(int j = 0; j < len2; j ++){
 8                 result[i + j] += (num1.charAt(len1 - 1 - i) - '0') * (num2.charAt(len2 - 1 - j) - '0');
 9             }
10         }
11         int upp = 0;
12         StringBuilder sb = new StringBuilder();
13         for(int i = 0; i < result.length; i ++){
14             int sum = result[i] + upp;
15             sb.insert(0, sum % 10);
16             upp = sum / 10;
17         }
18         if(upp != 0) sb.insert(0, upp);
19         return sb.toString();
20     }
21 }

 

posted on 2013-09-26 07:36  Step-BY-Step  阅读(212)  评论(0编辑  收藏  举报

导航