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.

要求:字符串表示的数字可能无穷大,并且非负。

class Solution {
private:
    vector<string> tempStrs;
public:
    string add(string num1,string num2)
    {
        int n1=num1.size();
        int n2=num2.size();
        int i=n1-1;int j=n2-1;
        string resStr;
        int jinwei=0;
        while (i>=0&&j>=0)
        {
            int temp=(num1[i]-‘0‘)+(num2[j]-‘0‘)+jinwei;
            jinwei=temp>=10?1:0;
            temp%=10;
            resStr.push_back(temp+‘0‘);
            --i;
            --j;
        }
        if(i>=0){
            while (i>=0)
            {
                int temp=(num1[i]-‘0‘)+jinwei;
                jinwei=temp>=10?1:0;
                temp%=10;
                resStr.push_back(temp+‘0‘);
                --i;
            }
        }
        else if(j>=0){
            while (j>=0)
            {
                int temp=(num2[j]-‘0‘)+jinwei;
                jinwei=temp>=10?1:0;
                temp%=10;
                resStr.push_back(temp+‘0‘);
                --j;
            }
        }
        if(jinwei!=0){
            resStr.push_back(jinwei+‘0‘);
        }
        reverse(resStr.begin(),resStr.end());
        return resStr;
    }
    string multiply(string num1, string num2) {
        if(num1.empty()||num2.empty()) return "";
        if(num1=="0"||num2=="0") return "0";
        int jinwei=0;
        int n1=num1.size();
        int n2=num2.size();
        if(n2>n1){
            string temp=num1;
            num1=num2;
            num2=temp;
            n1=num1.size();
            n2=num2.size();
        }
        int time=0;
        string tempStr;
        string res;
        for(int i=n2-1;i>=0;--i)
        {
            tempStr.clear();
            jinwei=0;
            for(int j=n1-1;j>=0;--j)
            {
                int temp=(num1[j]-‘0‘)*(num2[i]-‘0‘)+jinwei;
                jinwei=temp>=10?(temp/10):0;
                temp=temp%10;
                tempStr.push_back(temp+‘0‘);
            }
            if(jinwei!=0){
                tempStr.push_back(jinwei+‘0‘);
            }
            reverse(tempStr.begin(),tempStr.end());
            for(int q=0;q<time;++q){
                tempStr.push_back(‘0‘);
            }
            tempStrs.push_back(tempStr);
            ++time;
        }
         if(tempStrs.size()>1){
            res=add(tempStrs[0],tempStrs[1]);
            for (int i=2;i<tempStrs.size();++i)
            {
                res=add(res,tempStrs[i]);
            }
            return res;
        }else
        {
            return tempStrs[0];
        }

    }
};
时间: 2024-10-25 14:12:17

Multiply Strings(字符串乘法模拟,包含了加法模拟)的相关文章

LeetCode OJ: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. 给出两个字符串,返回对应数字想乘后的字符串,由于这个字符串可能很大,所以不能采用一般的乘法,这里用的方法是模拟手工的乘法运算,算法 本身很简单,就是当时写的时候有些很小的细节搞错了,找了

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(

[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的数值范围的约束,那么我们该如何来计算

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

Multiply Strings 字符串相乘

http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/48055617 class Solution { public: string multiply(string num1, string num2) { int len1 = num1.length(); int len2 = num2.length(); vector<int> res(len1+len2,

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

43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. LeetCode43. Multiply Strings中等 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例?2: 输入: num1 = "123", num2 = "456&q

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. 第一眼看到这个题目,潜意识里觉得直接将字符串转换为数字相乘,然后将结果再转换为字符串,难道这题考的是字符串与数值之间的转换? 细看,发现数字可能非常大,那么问题来了:这个数据类型怎么定义

每日算法之三十四:Multiply Strings

大数相乘,分别都是用字符串表示的两个大数,求相乘之后的结果表示. 首先我们应该考虑一下测试用例会有哪些,先准备测试用例对防御性编程会有比较大的帮助,能够考虑一些极端情况.有以下几种用例: 1)"0","0" 2)"0","879127346783" 其中一个是零 3)"as234","123343"  存在非法字符 4)"000000000000001234",&qu

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * 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转换