Given two numbers represented as strings, return multiplication of the numbers as a string. Note that the numbers can be arbitrarily large and are non-negative.
we can simulate the process as the way we do multiplication by hand: sum up all products at each position, leave a single digit there and add carry to a higher position. Note that if the number of digits of the two numbers ism and n , respectively, the number of digits of their product is normally m+n or m+n−1 except the case when either is zero.
Time: O(mn)
we can simulate the process as the way we do multiplication by hand: sum up all products at each position, leave a single digit there and add carry to a higher position. Note that if the number of digits of the two numbers is
Time: O(mn)
public String multiply(String num1, String num2) {
if (num1 == null || num1.length() == 0 ||
num2 == null || num2.length() == 0)
return "";
if (num1.equals("0") || num2.equals("0")) // Either one is 0
return "0";
int m = num1.length(), n = num2.length();
// Multiply single digit of each number and add up products at each position
int[] prods = new int[m+n];
for (int i = n-1; i >= 0; i--)
for (int j = m-1; j >= 0; j--)
prods[i+j+1] += (num2.charAt(i)-'0') * (num1.charAt(j)-'0');
// Keep a single digit at each position and add carry to a higher position
StringBuilder result = new StringBuilder();
for (int i = n+m-1; i >= 0; i--) {
result.insert(0, prods[i]%10);
if (i > 0)
prods[i-1] += prods[i] / 10; // Carry
}
// Get rid of one leaing "0" (if any)
if (result.charAt(0) == '0')
result.deleteCharAt(0);
return result.toString();
}
Read full article from LeetCode - Multiply Strings | Darren's Blog
No comments:
Post a Comment