forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyTwoStrings.java
More file actions
28 lines (27 loc) · 984 Bytes
/
MultiplyTwoStrings.java
File metadata and controls
28 lines (27 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution {
public String multiply(String num1, String num2) {
// Start typing your Java solution below
// DO NOT write main() function
int[] product = new int[num1.length() + num2.length()];
//in case overflow
if(num1.equals("0") || num2.equals("0"))
return "0";
for(int i = num1.length() - 1; i >= 0; --i){
for(int j = num2.length() - 1; j >= 0; --j){
int a = num1.charAt(i) - 48, b = num2.charAt(j) - 48;
product[i + j + 1] += a * b;
}
}
int carry = 0;
StringBuffer sb = new StringBuffer();
for(int i = product.length - 1; i >= 1; --i){
int sum = product[i] + carry;
product[i] = sum % 10;
carry = sum / 10;
sb.append(product[i]);
}
if(carry != 0)
sb.append(carry);
return sb.reverse().toString();
}
}