Skip to content

Commit

Permalink
5/15
Browse files Browse the repository at this point in the history
  • Loading branch information
KidJoe1412 committed May 15, 2023
1 parent 0e106bc commit 951d346
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/leetcode/Q322.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* 原题:<a href="https://leetcode.cn/problems/coin-change/description/"></a>
* ******************************************************
*/
class Solution {
class Q322 {
int[] dp;
public int coinChange(int[] coins, int amount) {
// 数组大小为 amount + 1,初始值也为 amount + 1
Expand Down
50 changes: 50 additions & 0 deletions src/leetcode/Q43.java
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();
}
}










0 comments on commit 951d346

Please sign in to comment.