-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyStrings.java
More file actions
48 lines (40 loc) · 1.18 KB
/
MultiplyStrings.java
File metadata and controls
48 lines (40 loc) · 1.18 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
package com.leetcode;
/**
* Created by jamylu on 2018/3/12.
* leetcode043
* 两个大整数的乘法
*/
public class MultiplyStrings {
public static void main(String[] args) {
String num1 = "123";
String num2 = "45";
System.out.println(multiply(num1, num2));
}
public static String multiply(String num1, String num2) {
int m = num1.length();
int n = num2.length();
int ans[] = new int[m + n];
int sum;
int mul;
int pos1;
int pos2;
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
pos1 = i + j;
pos2 = i + j + 1;
sum = mul + ans[pos2];
ans[pos1] += sum / 10;// 下一次循环的pos2位,累加的值
ans[pos2] = sum % 10;
}
}
StringBuilder sb = new StringBuilder();
for (int item : ans) {
if (sb.length() == 0 && item == 0) {
} else {
sb.append(item);
}
}
return sb.length() == 0 ? "0" : sb.toString();
}
}