-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiplyStrings.java
More file actions
62 lines (50 loc) · 1.94 KB
/
MultiplyStrings.java
File metadata and controls
62 lines (50 loc) · 1.94 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Given two non-negative integers num1 and num2 represented as strings,
// return the product of num1 and num2, also represented as a string.
// See: https://leetcode.com/problems/multiply-strings/
package leetcode.string;
public class MultiplyStrings {
private StringBuilder res = new StringBuilder();
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0"))
return "0";
String tail = "";
String ans = "0";
for (int i = num1.length() - 1; i >= 0; i--) {
res.setLength(0);
int n1 = num1.charAt(i) - 48;
int carry = 0;
for (int j = num2.length() - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - 48;
res.append((n1 * n2 + carry) % 10);
carry = (n1 * n2 + carry) / 10;
}
if (carry > 0)
res.append(carry);
String curr = res.reverse().toString() + tail;
tail += "0";
ans = add(ans, curr);
}
return ans;
}
private String add(String num1, String num2) {
res.setLength(0);
int l1 = num1.length() - 1, l2 = num2.length() - 1;
int carry = 0;
while (l1 >= 0 || l2 >= 0) {
int n1 = l1 >= 0 ? num1.charAt(l1--) - 48 : 0;
int n2 = l2 >= 0 ? num2.charAt(l2--) - 48 : 0;
res.append((n1 + n2 + carry) % 10);
carry = (n1 + n2 + carry) / 10;
}
if (carry > 0)
res.append(carry);
return res.reverse().toString();
}
public static void main(String[] args) {
MultiplyStrings sln = new MultiplyStrings();
System.out.println(sln.multiply("0", "0")); // 6
System.out.println(sln.multiply("0", "10")); // 6
System.out.println(sln.multiply("2", "3")); // 6
System.out.println(sln.multiply("123", "456")); // 56088
}
}