码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 43: Multiply Strings

时间:2017-11-08 13:32:09      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:rect   result   miss   func   directly   end   present   problem   color   

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

 

 Notes: the problem is not hard but complex. Before coding, we should think out what kind of input this function need to handle, eg: what if one of them is "0".

 Solution 1: work but too much code
 
 1 public class Solution {
 2     public string Multiply(string num1, string num2) {
 3         if (num1 == "0" || num2 == "0") return "0";
 4         
 5         string result = "";
 6         var weight = new StringBuilder();
 7         
 8         for (int i = num1.Length - 1; i >= 0; i--)
 9         {
10             var n1 = (int)num1[i] - (int)0;
11             if (n1 != 0)
12             {
13                 int advance = 0;
14                 var sb = new StringBuilder();
15 
16                 for (int j = num2.Length - 1; j >= 0; j--)
17                 {
18                     var n2 = (int)num2[j] - (int)0;
19 
20                     var total = n1 * n2 + advance;
21                     sb.Insert(0, total % 10);
22                     advance = total / 10;
23                 }
24 
25                 if (advance != 0)
26                 {
27                     sb.Insert(0, advance);
28                 }
29 
30                 sb.Append(weight);
31 
32                 result = Add(result, sb.ToString());
33             }
34             
35             weight.Append(0);
36         }
37         
38         return result;
39     }
40     
41     private string Add(string a, string b)
42     {
43         if (a.Length == 0) return b;
44         if (b.Length == 0) return a;
45         
46         var sb = new StringBuilder();
47         int i = a.Length - 1, j = b.Length - 1, advance = 0;
48         while (i >= 0 || j >= 0)
49         {
50             if (i >= 0 && j >= 0)
51             {
52                 var n1 = (int)a[i] - (int)0;
53                 var n2 = (int)b[j] - (int)0;
54                 
55                 var total = n1 + n2 + advance;
56                 sb.Insert(0, total % 10);
57                 advance = total / 10;
58             }
59             else if (i >= 0)
60             {
61                 var n1 = (int)a[i] - (int)0;
62                 
63                 var total = n1 + advance;
64                 sb.Insert(0, total % 10);
65                 advance = total / 10;
66             }
67             else
68             {
69                 var n1 = (int)b[j] - (int)0;
70                 
71                 var total = n1 + advance;
72                 sb.Insert(0, total % 10);
73                 advance = total / 10;
74             }
75             
76             i--;
77             j--;
78         }
79         
80         if (advance != 0)
81         {
82             sb.Insert(0, advance);
83         }
84         
85         return sb.ToString();
86     }
87 }

 

Solution 2: this beats 100% c# submissions. We know the length of the product is between len1 + len2 -1 to len1 + len2, for example: 100 * 100 = 10000, 999 * 999 = 998001.

 1 public class Solution {
 2     public string Multiply(string num1, string num2) {
 3         int len1 = num1.Length;
 4         int len2 = num2.Length;
 5         
 6         if (len1 == 0 || len2 == 0)
 7         {
 8             return len1 == 0 ? num2 : num1;
 9         }
10         
11         if (num1 == "0" || num2 == "0") return "0";
12         
13         int[] result = new int[len1 + len2];
14         for (int i = len1 - 1; i >= 0; i--)
15         {
16             for (int j = len2 - 1; j >= 0; j--)
17             {
18                 int d1 = (int)num1[i] - (int)0;
19                 int d2 = (int)num2[j] - (int)0;
20                 
21                 int m = d1 * d2 + result[i + j + 1];
22                 result[i + j + 1] = m % 10;
23                 result[i + j] += m / 10;                
24             }
25         }
26         
27         StringBuilder sb = new StringBuilder();
28         if (result[0] != 0) sb.Append(result[0]);
29         
30         for (int i = 1; i < result.Length; i++)
31         {
32             sb.Append(result[i]);
33         }
34         
35         return sb.ToString();
36     }
37 }

 

Leetcode 43: Multiply Strings

标签:rect   result   miss   func   directly   end   present   problem   color   

原文地址:http://www.cnblogs.com/liangmou/p/7803323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!