leetcode-43 Multiply Strings

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38340127/article/details/89712879

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 num2 is < 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 integerdirectly.

实现大数乘法:最通俗的方法 和计算乘法一样 每一位数据相乘然后相加,具体实现如下:

    public String multiply(String num1, String num2) {
        String res = "";
        int a = num1.length();
        int b = num2.length();

        if (a == 1 || b == 1) {
            StringBuffer tmp = new StringBuffer();
            if (a < b) {
                String tmpAa = num2;
                num2 = num1;
                num1 = tmpAa;
            }
            if(Integer.valueOf(num2)==0) {
    return "0";
}
            int t = 0;
            for (int length = num1.length()-1; length >=0; length--) {
                int tmpRes = (num1.charAt(length) - '0') * (Integer.valueOf(num2)) + t;
                tmp.append(tmpRes % 10);
                t = tmpRes / 10;
            }
            
            if(t != 0) {
                tmp.append(t);
            }
            tmp.reverse();
            return tmp.toString();
        }

        for (int j = num2.length()-1; j >=0 ; j--) {

            res = mult(res, multiply(num1, num2.substring(j, j + 1)),num2.length()-1-j);

        }
        return res.toString();
    }
    
    
    public String mult(String num1,String num2,int j) {
        if(num2=="0") {
            return num1;
        }
        for(int a=1;a<=j;a++) {
            num2=num2+"0";
        }
        if(num1.length() == 0) {
            return num2;
        }
        StringBuffer res = new StringBuffer();
        int t = 0;
        int b = num1.length()-1;
        for(int i=num2.length()-1;i>=0;i--) {
            int resV = (num2.charAt(i)-'0')+t+(b>=0?(num1.charAt(b--)-'0'):0);
            res.append(resV%10);
            t = resV/10;

        }
        if(t != 0) {
            res.append(t);
        }
        res.reverse();
        return res.toString();
    }
    

方法二:对上述方法进行优化

    public String multiplyBetter(String num1, String num2) {
        StringBuffer resv = new StringBuffer();
        int[] res = new int[num1.length()+num2.length()];
        for(int i=0;i<num1.length();i++) {
            for(int j=0;j<num2.length();j++) {
                int a = num1.charAt(num1.length()-1-i)-'0';
                int b = num2.charAt(num2.length()-1-j)-'0';
                int tmpRes =res[i+j]+ a*b;
                res[i+j] = tmpRes%10;
                res[i+j+1] += tmpRes/10;
            }
        }
        boolean first = true;
        for(int i=0;i<res.length;i++) {
            if(res[res.length-1-i]==0 && first) {
                continue;
            }else {
                first = false;
                resv.append(res[res.length-1-i]);
            }
        }
        if(resv.length()==0) {
            return "0";
        }
        return resv.toString();
    }

猜你喜欢

转载自blog.csdn.net/qq_38340127/article/details/89712879
今日推荐