Leetcode 43: Multiply Strings
Posted Keep walking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 43: Multiply Strings相关的知识,希望对你有一定的参考价值。
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - 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的主要内容,如果未能解决你的问题,请参考以下文章
<LeetCode OJ> 43. Multiply Strings