<LeetCode OJ> 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: The numbers can be arbitrarily large and are non-negative.

Subscribe to see which companies asked this question

Hide Tags

Math String

Show Similar Problems

分析:

代码注释已经很详细,模拟手算即可

//思路首先:
//模拟手算过程即可
class Solution {
public:
    string multiply(string num1, string num2) {
        int allLen=num1.size()+num2.size();
        vector<int> tmpresult(allLen,0);
        string result(allLen,'0');
        //模拟手算从最后一位开始处理
        for(int i=num1.size()-1;i>=0;i--)
        {
            int n1=num1[i]-'0';
            for(int j=num2.size()-1;j>=0;j--)
            {
                int n2=num2[j]-'0';
                tmpresult[i+j+1]+= n1*n2;
            }
        }
        //进位
        for(int i=allLen-1;i>0;i--)
        {
            while(tmpresult[i]>9)
            {
                tmpresult[i-1]+=tmpresult[i]/10;
                tmpresult[i]%=10;
            }
        }
        //转换成字符串
        for(int i=allLen-1;i>=0;i--)
            result[i]=tmpresult[i]+'0';

        if(result.find_first_not_of('0') == string::npos)
            return "0";//排除全0的情况
        return result.substr(result.find_first_not_of('0'),string::npos);
    }
};

注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50575524

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-11-08 06:11:01

<LeetCode OJ> 43. Multiply Strings的相关文章

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

43. Multiply Strings 字符串相乘

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading z

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题意分析&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

[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

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 multiply(string& num, char ch){ int n = ch - '0'; string s; int carry = 0; int x; for(

43. Multiply Strings java solutions

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 su

【一天一道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 us

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&