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.

题目当中主要是需要两个大数的乘法,一般大数都是用字符串进行保存

代码比较简单,主要是利用了一个vector反向的存储了计算的结果,然后reverse到string当中进行输出

这里当然可以直接定义一个string然后其中进行操作,那么就会出现很多char和int之间的转换,感觉比较麻烦

class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1=="0" || num2=="0") return "0";
        int length1 = num1.size();
        int length2 = num2.size();
        vector<int> mulResult(length1+length2,0);//<used to restore the multi result
        int i = 0,j = 0;
        int temp = 0;
        int nextNum = 0; //<10位数
        int index = 0;
        int n1;
        int n2;
        for(i = length2-1; i >= 0; i--)
        {
            index = (length2-1)-i;
            n2 = num2[i]-'0';
            nextNum = 0;
            for(j = length1-1; j >= 0;j--)
            {
                n1 = num1[j]-'0';
                temp = n2*n1;
                temp += nextNum; //<加上上一次进位的数
                temp += mulResult[index];  //<加上之前计算的结果
                mulResult[index] = (temp%10);
                nextNum = temp/10;
                index++;
            }
            if(nextNum>0)
            {
                mulResult[index] = nextNum;
                index++;
            }

        }
        string result = "";
        for(i = index-1; i>=0;i--)
        {
            result += ('0'+ mulResult[i]);
        }
        return result;
    }
};
时间: 2024-12-28 01:30:59

LeetCode-Multiply Strings实现大数的乘法的相关文章

[leetcode]Multiply Strings @ Python

原题地址:https://oj.leetcode.com/problems/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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是

LeetCode: Multiply Strings [042]

[题目] 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. [题意] 给定用字符串表示的整数,返回两个数的乘积结果字符串.两个数字都非负,且能任意大. [思路] 1. 考虑其中一个数是0的情况 2. 模拟乘法运算过程 维护一个vecto

leetcode Multiply Strings

题目连接 https://leetcode.com/problems/multiply-strings/ Multiply Strings Description 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. 普通写法: class

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "56088" 说明: num1 和 num2 的长度小于110. num1 和 nu

[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. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

LeetCode: Multiply Strings. Java

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(String num1, String num2) { int n = n

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. 原题链接:https://oj.leetcode.com/problems/multiply-strings/ 按照乘法原理,从个位开始一位一位相乘相加. public class Mu

【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. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2