-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e106bc
commit 951d346
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package leetcode; | ||
/** | ||
* 题干:字符串相乘 | ||
* 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 | ||
* 注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。 | ||
* ****************************************************** | ||
* 解法: | ||
* ****************************************************** | ||
* 原题:<a href="https://leetcode.cn/problems/multiply-strings/description/"></a> | ||
* ****************************************************** | ||
*/ | ||
class Solution { | ||
public String multiply(String num1, String num2) { | ||
if (num1.equals("0") || num2.equals("0")){ | ||
return "0"; | ||
} | ||
int m = num1.length(),n = num2.length(); | ||
int[] ansArr = new int[m + n]; | ||
for (int i = m - 1; i >= 0; i--) { | ||
int x = num1.charAt(i) - '0'; | ||
for (int j = n - 1; j >= 0; j--) { | ||
int y = num2.charAt(j) - '0'; | ||
ansArr[i + j + 1] += x * y; | ||
|
||
} | ||
} | ||
// 超出10的往高位进一 | ||
for (int i = m + n - 1; i > 0; i--) { | ||
ansArr[i - 1] += ansArr[i] / 10; | ||
ansArr[i] %= 10; | ||
} | ||
// index表示从第几位开始把数组转化为字符串 | ||
int index = ansArr[0] == 0 ? 1 : 0; | ||
StringBuilder sb = new StringBuilder(); | ||
while (index < m + n){ | ||
sb.append(ansArr[index++]); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|