LeetCode 43. Multiply Strings(高精度)

题目来源:https://leetcode.com/problems/multiply-strings/

问题描述

43. Multiply Strings

Medium

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"

Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"

Output: "56088"

Note:

  1. The length of both num1 and num2is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

------------------------------------------------------------

题意

给定两个字符串num1num2表示两个正整数,求num1num2的乘积,同样以字符串返回。

------------------------------------------------------------

思路

高精度。代码量略大。当然Java自带BigInteger类,可以直接解决这个问题,当然我相信出题者的本意不是如此。

自己写一个BigInteger类,属性有表示位数的整数n和存储每个位的数组digits(digits[0]存储大整数的最低位,与字符串表示正好相反). 方法有:

1. 从字符串的改造方法public BigInteger(String num),将字符串表示转化为数组存储,同时计算大整数的位数;

2. 默认无参构造方法public BigInteger(),构造一个大整数0;

3. public void clearHigher0()方法,去除大整数高位冗余的0;

4. public BigInteger mul(BigInteger rhs)方法,大整数乘法,本题的核心代码。基于数组的大整数乘法类似竖式乘法,不过逻辑更简单一些。具体来说,就是乘数的第i位和被乘数的第j位相乘加到结果的第(i+j)位上去,如果乘出来大于10,则发生进位,进位加到结果的第(i+j+1)位上去,余数留在结果的第(i+j)位。

5. public String toString()方法,重写toString方法,从数组存储方式得到字符串表示。

我之前写过一个C++版的高精度,包含大整数加法、减法和乘法,有需要请移步:2019年清华软院推免考试(校外直博&校内硕/博) 第一题——超长整数相乘(Multiply)

------------------------------------------------------------

代码

class Solution {
    class BigInteger {
        public int[] digits;
        public int n;
        
        public BigInteger(String num)
        {
            n = num.length();
            digits = new int[n];
            for (int i=0; i<n; i++)
            {
                digits[i] = Character.getNumericValue(num.charAt(n - 1 - i));
            }
        }
        
        public BigInteger()
        {
            n = 1;
            digits = new int[1];
            digits[0] = 0;
        }
        
        public void clearHigher0()
        {
            for (int i = n-1; i>=1; i--)
            {
                if (digits[i] == 0)
                {
                    n--;
                }
                else
                {
                    break;
                }
            }
        }
        
        public BigInteger mul(BigInteger rhs)
        {
            BigInteger res = new BigInteger();
            int nr = rhs.n;
            res.n = n + nr;
            res.digits = new int[res.n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < nr; j++)
                {
                    res.digits[i+j] += digits[i] * rhs.digits[j];
                    if (res.digits[i+j] >= 10)
                    {
                        res.digits[i+j+1] += res.digits[i+j] / 10;
                        res.digits[i+j] = res.digits[i+j] % 10;
                    }
                }
            }
            res.clearHigher0();
            return res;
        }
        
        public String toString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = n-1; i >= 0; i--)
            {
                sb.append(String.valueOf(digits[i]));
            }
            return sb.toString();
        }
    }
    
    public String multiply(String num1, String num2) {
        BigInteger Num1 = new BigInteger(num1), Num2 = new BigInteger(num2);
        return Num1.mul(Num2).toString();
    }
}

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/88965291