Skip to content

Commit c3e974f

Browse files
author
王俊超
committed
commit
1 parent 98248e6 commit c3e974f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-09-28 15:23
4+
**/
5+
public class Solution {
6+
public String multiply(String num1, String num2) {
7+
int m = num1.length(), n = num2.length();
8+
int[] pos = new int[m + n];
9+
10+
int mul;
11+
int index;
12+
int temp;
13+
for (int i = m - 1; i >= 0; i--) {
14+
for (int j = n - 1; j >= 0; j--) {
15+
// 求积
16+
mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
17+
// 积的最低位所在的位置
18+
index = i + j + 1;
19+
20+
// 最低低求和
21+
pos[index] += mul;
22+
23+
// 做进位处理
24+
while (pos[index] > 9) {
25+
temp = pos[index];
26+
pos[index] = temp % 10;
27+
--index;
28+
pos[index] += temp / 10;
29+
}
30+
31+
}
32+
}
33+
34+
StringBuilder sb = new StringBuilder();
35+
for (int p : pos) {
36+
if (!(sb.length() == 0 && p == 0)) {
37+
sb.append(p);
38+
}
39+
}
40+
return sb.length() == 0 ? "0" : sb.toString();
41+
}
42+
}

0 commit comments

Comments
 (0)