0
点赞
收藏
分享

微信扫一扫

leetcode刷题计划Day18

gy2006_sw 2022-04-24 阅读 103

文章目录

43. 字符串相乘【中等】

https://leetcode-cn.com/problems/multiply-strings/
在这里插入图片描述

class Solution {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int[] res = new int[num1.length() + num2.length()];
        for (int i = num1.length() - 1; i >= 0; i--) {
            int n1 = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int n2 = num2.charAt(j) - '0';
                int sum = (res[i + j + 1] + n1 * n2);  // res[i+j+1]是上一轮第i+j+1位相加的结果
                res[i + j + 1] = sum % 10;
                res[i + j] += sum / 10;
            }
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < res.length; i++) {
            if (i == 0 && res[i] == 0) continue;
            result.append(res[i]);
        }
        return result.toString();
    }
}

322. 零钱兑换【中等】

https://leetcode-cn.com/problems/coin-change/

41. 缺失的第一个正数【困难】

https://leetcode-cn.com/problems/first-missing-positive/

举报

相关推荐

0 条评论