【一天一道LeetCode】#43. Multiply Strings

一天一道LeetCode系列

(一)题目

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.

Converting the input string to integer is NOT allowed.

You should NOT use internal library such as BigInteger.

(二)解题

两个string代表的数字相乘,主要就是要求写出大数相乘的代码,具体看注释

/*
本题的思路主要是:num1的第i位和num2的第j位相乘等于乘积的第i+j位
m位数和n位数相乘的结果最大位m+n位!
1.为了便于理解,将两个string首先反转
2.两个for循环,从num1的0位开始,依次乘上num2的0-size位,注意考虑到进位的问题
3.对于结果ret,先反转,如果全为0,则直接返回0,否则去掉前面连续的0,即为最后的结果
*/
class Solution {
public:
    string multiply(string num1, string num2) {
        reverse(num1.begin(),num1.end());//反转num1
        reverse(num2.begin(),num2.end());//反转num2
        string ret(num1.size()+num2.size(),‘0‘);
        for(int i = 0 ; i < num1.size() ; i++)
        {
            int carry = 0;//处理进位
            int j = 0;
            for(; j < num2.size() ; j++)
            {
                carry += (ret[i+j]-‘0‘) + (num1[i]-‘0‘)*(num2[j]-‘0‘);
                ret[i+j] = (‘0‘+carry%10) ;
                carry /=10;
            }
            if(carry!=0)//处理最后的进位
            {
                ret[i+j] = (‘0‘+carry);
            }
        }
       //对最后的字符串进行处理
        reverse(ret.begin(),ret.end());
        for(auto iter=ret.begin() ; iter!=ret.end() ; )
        {
            if(*iter == ‘0‘&&ret.size()>1) iter = ret.erase(iter);//如果为‘0’则删除
            else break;//第一位不为‘0’的数就退出
        }
        return ret;
    }
};
时间: 2024-08-24 15:38:38

【一天一道LeetCode】#43. Multiply Strings的相关文章

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

19.2.4 [LeetCode 43] Multiply Strings

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&

leetcode 43 Multiply Strings 大数相乘

感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){ return "0"; } int len1 = num1.length(); int len2 = num2.length(); int len = len1+len2; int[] arr =

&lt;LeetCode OJ&gt; 43. Multiply Strings

43. Multiply Strings My Submissions Question Total Accepted: 51859 Total Submissions: 231017 Difficulty: Medium 以字符串的形式给定两个数字,返回相乘的结果,注意:结果也是字符串,因为数字可能很大 Given two numbers represented as strings, return multiplication of the numbers as a string. Note

43. Multiply Strings(js)

43. Multiply Strings 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: Inp

【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题意分析&amp;解答】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. 题意分析: 本题是求两个用string表示的非负大数的乘积,乘数可以是任意大小. 解答: 可以用一个临时List表示乘积的每一位,然后对两个乘数每一位两两相乘,并将结果填到相应的List