Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc284818

Browse files
committed
feat: add 043
1 parent1e9ae21 commitc284818

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
|17|[Letter Combinations of a Phone Number][017]|String, Backtracking|
6565
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
6666
|33|[Search in Rotated Sorted Array][033]|Arrays, Binary Search|
67+
|43|[Multiply Strings][043]|Math, String|
6768
|554|[Brick Wall][554]|Hash Table|
6869

6970

@@ -124,6 +125,7 @@
124125
[017]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
125126
[019]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
126127
[033]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/033/README.md
128+
[043]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/043/README.md
127129
[554]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/554/README.md
128130

129131
[004]:https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md

‎note/043/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#[Multiply Strings][title]
2+
3+
##Description
4+
5+
Given two non-negative integers`num1` and`num2` represented as strings, return the product of`num1` and`num2`.
6+
7+
**Note:**
8+
9+
1. The length of both`num1` and`num2` is < 110.
10+
2. Both`num1` and`num2` contains only digits`0-9`.
11+
3. Both`num1` and`num2` does not contain any leading zero.
12+
4. You**must not use any built-in BigInteger library** or**convert the inputs to integer** directly.
13+
14+
**Tags:** Math, String
15+
16+
17+
##思路
18+
19+
题意是让你计算两个非负字符串的乘积,我们模拟小学数学的方式来做,一位一位模拟计算,再各位累加。
20+
21+
```java
22+
classSolution {
23+
publicStringmultiply(Stringnum1,Stringnum2) {
24+
if (num1.equals("0")|| num2.equals("0"))return"0";
25+
int l1= num1.length(), l2= num2.length(), l= l1+ l2;
26+
char[] ans=newchar[l];
27+
char[] c1= num1.toCharArray();
28+
char[] c2= num2.toCharArray();
29+
for (int i= l1-1; i>=0;--i) {
30+
int c= c1[i]-'0';
31+
for (int j= l2-1; j>=0;--j) {
32+
ans[i+ j+1]+= c* (c2[j]-'0');
33+
}
34+
}
35+
for (int i= l-1; i>0;++i) {
36+
if (ans[i]>9) {
37+
ans[i-1]+= ans[i]/10;
38+
ans[i]%=10;
39+
}
40+
}
41+
StringBuilder sb=newStringBuilder();
42+
int i=0;
43+
for (; ;++i)if (ans[i]!=0)break;
44+
for (; i< ans.length;++i) sb.append((char) (ans[i]+'0'));
45+
return sb.toString();
46+
}
47+
}
48+
```
49+
50+
51+
##结语
52+
53+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
54+
55+
56+
57+
[title]:https://leetcode.com/problems/multiply-strings
58+
[ajl]:https://github.com/Blankj/awesome-java-leetcode
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.blankj.medium._043;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/10/17
8+
* desc :
9+
* </pre>
10+
*/
11+
publicclassSolution {
12+
publicStringmultiply(Stringnum1,Stringnum2) {
13+
if (num1.equals("0") ||num2.equals("0"))return"0";
14+
intl1 =num1.length(),l2 =num2.length(),l =l1 +l2;
15+
char[]ans =newchar[l];
16+
char[]c1 =num1.toCharArray();
17+
char[]c2 =num2.toCharArray();
18+
for (inti =l1 -1;i >=0; --i) {
19+
intc =c1[i] -'0';
20+
for (intj =l2 -1;j >=0; --j) {
21+
ans[i +j +1] +=c * (c2[j] -'0');
22+
}
23+
}
24+
for (inti =l -1;i >0; --i) {
25+
if (ans[i] >9) {
26+
ans[i -1] +=ans[i] /10;
27+
ans[i] %=10;
28+
}
29+
}
30+
StringBuildersb =newStringBuilder();
31+
inti =0;
32+
for (; ; ++i)if (ans[i] !=0)break;
33+
for (;i <ans.length; ++i)sb.append((char) (ans[i] +'0'));
34+
returnsb.toString();
35+
}
36+
37+
publicstaticvoidmain(String[]args) {
38+
Solutionsolution =newSolution();
39+
System.out.println(solution.multiply("132","19"));
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp