Skip to content

Commit b82c796

Browse files
committed
fd
1 parent c9f9ff5 commit b82c796

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

solution/src/main/java/com/inuker/solution/MultiplyStrings.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,23 @@
77
public class MultiplyStrings {
88

99
public String multiply(String num1, String num2) {
10-
int len1 = num1.length(), len2 = num2.length();
11-
int[] result = new int[len1 + len2];
12-
for (int i = len1 - 1; i >= 0; i--) {
13-
for (int j = len2 - 1; j >= 0; j--) {
14-
int res = result[i + j + 1] + (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
15-
result[i + j + 1] = res % 10;
16-
result[i + j] += res / 10;
10+
int l1 = num1.length(), l2 = num2.length();
11+
int[] result = new int[l1 + l2];
12+
for (int i = l1 - 1; i >= 0; i--) {
13+
for (int j = l2 - 1; j >= 0; j--) {
14+
int n1 = num1.charAt(i) - '0';
15+
int n2 = num2.charAt(j) - '0';
16+
int n = result[i + j + 1] + n1 * n2;
17+
result[i + j + 1] = n % 10;
18+
result[i + j] += n / 10;
1719
}
1820
}
19-
2021
StringBuilder sb = new StringBuilder();
21-
for (int n : result) {
22-
// 这里要去掉头部的"0"
23-
if (n == 0 && sb.length() == 0) {
24-
continue;
22+
for (int i = 0; i < result.length; i++) {
23+
if (result[i] != 0 || sb.length() > 0) {
24+
sb.append(result[i]);
2525
}
26-
sb.append(n);
2726
}
28-
29-
/**
30-
* 这里要注意如果是空要返回0
31-
*/
3227
return sb.length() == 0 ? "0" : sb.toString();
3328
}
3429
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,27 @@
2828
public class main {
2929

3030
public static void main(String[] args) {
31-
System.out.println(minWindow("ADOBECODEBANC", "ABC"));
31+
System.out.println(multiply("99", "100"));
3232
}
3333

34-
public static String minWindow(String s, String t) {
35-
int[] tc = new int[256], sc = new int[256];
36-
for (char c : t.toCharArray()) {
37-
tc[c]++;
38-
}
39-
int count = 0, min = Integer.MAX_VALUE, start = 0;
40-
for (int i = 0, j = 0; j < s.length(); j++) {
41-
char c = s.charAt(j);
42-
if (++sc[c] <= tc[c]) {
43-
++count;
34+
public static String multiply(String num1, String num2) {
35+
int l1 = num1.length(), l2 = num2.length();
36+
int[] result = new int[l1 + l2];
37+
for (int i = l1 - 1; i >= 0; i--) {
38+
for (int j = l2 - 1; j >= 0; j--) {
39+
int n1 = num1.charAt(i) - '0';
40+
int n2 = num2.charAt(j) - '0';
41+
int n = result[i + j + 1] + n1 * n2;
42+
result[i + j + 1] = n % 10;
43+
result[i + j] += n / 10;
4444
}
45-
if (count == t.length()) {
46-
for ( ; i < j; i++) {
47-
char cc = s.charAt(i);
48-
if (sc[cc] <= tc[cc]) {
49-
break;
50-
}
51-
sc[cc]--;
52-
}
53-
if (j - i + 1 < min) {
54-
min = j - i + 1;
55-
start = i;
56-
}
45+
}
46+
StringBuilder sb = new StringBuilder();
47+
for (int i = 0; i < result.length; i++) {
48+
if (result[i] != 0 || sb.length() > 0) {
49+
sb.append(result[i]);
5750
}
5851
}
59-
return min == Integer.MAX_VALUE ? "" : s.substring(start, start + min);
52+
return sb.length() == 0 ? "0" : sb.toString();
6053
}
6154
}

0 commit comments

Comments
 (0)