FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc43.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc43.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
28 lines (28 loc) · 1.13 KB
Breadcrumbs
leetcode
/
code
/
lc43.java
Copy path
File metadata and controls
28 lines (28 loc) · 1.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
code
;
/*
* 43. Multiply Strings
* 题意:大数相乘
* 难度:Medium
* 分类:Math, String
* 思路:每个位置上的数字都相乘,把位置排好,加起来
* https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation
* Tips:挺难的,好多细节
*/
public
class
lc43
{
public
String
multiply
(
String
num1
,
String
num2
) {
int
[]
res
=
new
int
[
num1
.
length
()+
num2
.
length
()];
//最高位 index 是 0
for
(
int
i
=
num1
.
length
()-
1
;
i
>=
0
;
i
--) {
//从后往前,解决重复进位
for
(
int
j
=
num2
.
length
()-
1
;
j
>=
0
;
j
--) {
int
a
=
i
+
j
;
int
b
=
i
+
j
+
1
;
int
mul
= (
num1
.
charAt
(
i
) -
'0'
) * (
num2
.
charAt
(
j
) -
'0'
);
mul
+=
res
[
b
];
//解决进位
res
[
b
] =
mul
%
10
;
//右边的直接=,因为上一步已经+了
res
[
a
] +=
mul
/
10
;
//左边+=
}
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
:
res
)
if
(!(
sb
.
length
()==
0
&&
i
==
0
))
sb
.
append
(
i
);
return
sb
.
length
() ==
0
?
"0"
:
sb
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL