LeetCode 字符串相乘(43题)

LeetCode 字符串相乘

@author:Jingdai
@date:2020.09.26

题目描述(43题)

给定两个以字符串形式表示的非负整数 num1num2,返回 num1num2 的乘积,乘积也表示为字符串形式。

  • 示例
输入: num1 = "745", num2 = "12"
输出: "8940"

不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理

思路

  • 方法1

    在这里插入图片描述

    如图,就是利用小学学习过的乘法,对 num2 从低位开始每一位与 num1 相乘,然后将所有的中间结果相加就是最后的答案。

    其中要注意的点就是除了第一位,后面的都要补零,而字符串相加很简单,直接看代码就行。

  • 方法2

    在这里插入图片描述

    如图,我们将 num1num2 的每一位都相乘,然后将相乘的结果加起来就是最后的结果。同时,这里就不用再利用字符串相加了,因为可以看到 num1 的第 i 位和 num2 的第 j 位相乘的结果一定是两位的,而且低位落在最后结果的 k=i+j+1 位上,而高位位于 k=i+j 位上,所以可以建立一个数组进行相加,把结果放入数组中对应的位置上。

    那应该建立多大的数组呢?这里我们要知道两个整数相乘的最大位数是两个乘数的位数和,而最小位数是两个乘数的位数和减一,比如最小的三位数 100 和最小的两位数 10 相乘 是 1000 是 4 位,而最大的三位数 999 和最大的两位数 99 相乘是 98901 是 5 位,那其他肯定是4-5位了,所以我们直接建立两个乘数的位数和那么大的数组就好了,最后判断如果第一位是 0 的话删去就行。

    还有不懂的点看代码应该就懂了。

代码

  • 方法1

    public String multiply(String num1, String num2) {
           
           
            
        if ("0".equals(num1) || "0".equals(num2)) {
           
           
            return "0";
        }
    
        String ans = "0";
        for (int i = 0; i < num2.length(); i++) {
           
           
            int index = num2.length() - i - 1;
            int tempMultiply = num2.charAt(index) - '0';
    
            StringBuilder tempRes = new StringBuilder();
            // add zero
            for (int j = 0; j < i; j++) {
           
           
                tempRes.append(0);
            }
    
            int tempAdd = 0;
            for (int j = num1.length() - 1; j >= 0; j--) {
           
           
                tempAdd += (num1.charAt(j) - '0') * tempMultiply;
                tempRes.append(tempAdd % 10);
                tempAdd /= 10; 
            }
            if (tempAdd != 0) {
           
           
                tempRes.append(tempAdd);
            }
            tempRes.reverse();
            ans = add(tempRes.toString(), ans);
        }
        return ans;
    }
    
    public String add(String num1, String num2) {
           
           
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int item = 0; 
        StringBuilder ans = new StringBuilder();
        while (i >= 0 || j >= 0 || item != 0) {
           
           
            if (i >= 0) {
           
           
                item += num1.charAt(i--) - '0';
            }
            if (j >= 0) {
           
           
                item += num2.charAt(j--) - '0';
            }
            ans.append(item % 10);
            item /= 10;
        }
        return ans.reverse().toString();
    }
    
  • 方法2

    public String multiply(String num1, String num2) {
           
           
            
        if ("0".equals(num1) || "0".equals(num2)) {
           
           
            return "0";
        } 
    
        int[] ans = new int[num1.length() + num2.length()];
    
        for (int i = num1.length() - 1; i >= 0; i--) {
           
           
            int tempN1 = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
           
           
                int tempAdd = tempN1 * (num2.charAt(j) - '0')
                    + ans[i+j+1];
                ans[i+j+1] = tempAdd % 10;
                ans[i+j] += tempAdd / 10;
            }
        }
    
        StringBuilder ansString = new StringBuilder();
    
        for (int i = 0; i < ans.length; i++) {
           
           
            if (i == 0 && ans[0] == 0) {
           
           
                continue;
            }
            ansString.append(ans[i]);
        }
    
        return ansString.toString();
    }
    

猜你喜欢

转载自blog.csdn.net/qq_41512783/article/details/108814727