leetcode之Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note: The numbers can be arbitrarily large and are non-negative.

这道题的意思是字符串表示的数字相乘。

没啥好说的直接付代码。

public String multiply(String num1, String num2) {
	    BigInteger temp1 = new BigInteger(num1);
	    BigInteger temp2 = new BigInteger(num2);
	    BigInteger result = temp1.multiply(temp2);
	    return result.toString();

	   }

  另外一种是实现:

public String multiply1(String num1, String num2) {
        if (num1 == null || num2 == null) {
            return null;
        }

        int len1 = num1.length(), len2 = num2.length();
        int len3 = len1 + len2;
        int i, j, product, carry;

        int[] num3 = new int[len3];
        for (i = len1 - 1; i >= 0; i--) {
            carry = 0;
            for (j = len2 - 1; j >= 0; j--) {
                product = carry + num3[i + j + 1] +
                    Character.getNumericValue(num1.charAt(i)) *
                    Character.getNumericValue(num2.charAt(j));
                num3[i + j + 1] = product % 10;
                carry = product / 10;
            }
            num3[i + j + 1] = carry;
        }

        StringBuilder sb = new StringBuilder();
        i = 0;

        while (i < len3 - 1 && num3[i] == 0) {
            i++;
        }

        while (i < len3) {
            sb.append(num3[i++]);
        }

        return sb.toString();
    }

  

时间: 2024-08-07 08:39:53

leetcode之Multiply Strings的相关文章

【LeetCode】Multiply Strings

Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 我借鉴了JackBauer的一些思想,将乘积逆序存放在int数组result中. 记num1当前为第ind1位(个位为0),num2当前为ind2位,则

LeetCode 043 Multiply Strings

题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 分析: 参考网址:http://blog.csdn.net/pickless/article/details/9235907 利用竖式的思想,

LeetCode (19) Multiply Strings

题目描述 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 对两组非负数字进行相乘,使用数组表示数字,且题目中说明数组很大,因此,因此不能直接将数组转换成数字相乘.这道题目是要求自己构造乘法的思路,需要注意的地方主要为进位的处理. 解题

Java [Leetcode 43]Multiply Strings

题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 解题思路: 设置数组记录单个位置相乘的结果,最后负责相加进位. 代码如下: public class Solution { public String multiply(St

[LeetCode#43]Multiply Strings

Problem: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. Analysis: The naive solution of this problem is to following the routine of multipli

【leetcode】Multiply Strings(middle)

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 思路:直观思路,就是模拟乘法过程.注意进位.我写的比较繁琐. string multiply(string num1, string num2) { vector<int> v1, v

Java for LeetCode 043 Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 解题思路一: BigInteger!!! JAVA实现如下: static public String multiply(String num1, String num2) { java

[C++]LeetCode: 69 Multiply Strings

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. Answer 1: 基础法 大整数乘法 根据小学手算乘法的规则,我们把每一位相乘,得到一个没有进位的临时结果,如图中间的一行红色数字就是临时结果,然后把临时结果从低位起一次进位.

【LeetCode】Multiply Strings 解题报告

[题目] Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. [解析] 题意:两个字符串表示的非负整数相乘,用字符串的形式返回积. 思路:逐位相乘. 关键:中间结果如何保存?如果用字符串保存中间结果,频繁该值不太方便,所以还是用整数数组保