leetcode 第42题 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.

就是实现大数乘法。嘿嘿,我先用long long直接乘试试手感,试过了不行。所以还是要用字符串表示才行。

我是这样做的,先实现两个子函数,一个是实现一个数和一个字符串的数相乘的结果。另一个是实现两个字符串相加的结果。这样,大数乘法就可以由其中一个大数的每一位和另一个大数相乘然后再相加,就是要注意分解一个大数的时候记得要在结果后面加上零。例如12乘以3,我们把12分成1和2分别和3相乘,然后1和3相乘的后面要加一个零,因为1处于12的十位数。

oj上居然用了148ms。不忍直视啊。写了那么长。。。虽然Accept了

class Solution {
public:
string singleMulti(string s, char a) // 给定一个字符串和一个字符,返回乘积(字符串形式)
{
    if (s.size() == 0)
        return s;
    if (a == ‘0‘)
        return "0";
    int c = a - ‘0‘;
    int up = 0;
    for (int i = s.size() - 1; i > -1; --i)
    {
        int tmp = s[i] - ‘0‘;
        int sum_now = up + c*tmp;
        s[i] = sum_now%10 + ‘0‘;
        up = sum_now/10;
    }
    if (up != 0)
        {char tmp = up + ‘0‘;
        s = tmp + s;}
    return s;
}
string sum42(string s1, string s2) // sum of two string
{
    if (s1.size() == 0)
        return s2;
    if (s2.size() == 0)
        return s1;
    string s = s1;
    int len1 = s1.size() - 1, len2 = s2.size() - 1, up = 0;
    while(len1 > -1 && len2 > -1)
    {
        int n1 = s1[len1] - ‘0‘;
        int n2 = s2[len2] - ‘0‘;
        int n3 = n1 + n2 + up;
        s[len1] = n3%10 + ‘0‘;
        up = n3/10;
        len1--;len2--;
    }
    while(len1 > -1)
    {
        int n1 = s1[len1] - ‘0‘;
        int n3 = n1 + up;
        s[len1] = n3%10 + ‘0‘;
        up = n3/10;
        len1--;
    }
    while(len2 > -1)
    {
        int n2 = s2[len2] - ‘0‘;
        int n3 = n2 + up;
        char tmp = n3%10 + ‘0‘;
        s = tmp + s;
        up = n3/10;
        len2--;
    }
    if(up)
    {
        char tmp = up + ‘0‘;
        s = tmp + s;
    }
    return s;
}
string multiply(string num1, string num2)
{
    if (num1 == "0" || num2 == "0")
        return "0";
    int len1 = num1.size()-1;
    string s;
    int cnt = 0;
    while(len1 > -1)
    {
        string tmp = "";
        int tn = cnt;
        while(tn)
        {
            tmp = ‘0‘ + tmp;
            tn--;
        }
        s = sum42(s,singleMulti(num2, num1[len1])+ tmp);
        len1--;
        cnt++;
    }
    return s;
}
};

由于自己的代码那么长,所以也学习了下别人的,例如这里利用下面的图,很好理解。将图贴出:

我们以289*758为例

按照这个图我写了下代码(跟原版主略有不同):

class Solution {
public:
string multiply(string num1, string num2)
{
    if (num1 == "0" || num2 == "0")
        return "0";
    string s = "";
    int len1 = num1.size(), len2 = num2.size();
    vector<int> container(len1+len2, 0); // 用来存表中红色部分值

    for (int i = 0; i < len1; ++i)
        for (int j = 0; j < len2; ++j)
        {
            container[i+j] += (num1[len1 - 1 - i]-‘0‘)*(num2[len2 - 1 - j]-‘0‘); // 注意标号
        }
    //处理进位
    int up = 0, cnt = 0;
    while(cnt<len1+len2)
    {
        container[cnt] += up;
        up = container[cnt]/10;
        container[cnt] %= 10;
        cnt++;
    }
    cnt--;
    while(cnt > -1 && !container[cnt])//不应该有的零去掉
    {
        cnt--;
    };
    while(cnt > -1) // 输出就是结果,注意方向被搞反了
    {
        char ch = container[cnt] + ‘0‘;
        s += ch;
        cnt--;
    }
    return s;
}
};

这个主要是注意存的方向,不要把小标弄混淆了。输出的时候也要注意,别搞反了。低于60ms过。

时间: 2024-12-31 05:08:53

leetcode 第42题 Multiply Strings的相关文章

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(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. 分析 计算两个字符串表示的非负大整数的乘积,结果仍然用字符串表示. 我们都熟悉笔算的整数乘积,按照被乘数逐位与乘数求积,保存进位:当被乘数换位时,结果递增一个数量级,与先前结果求和

LeetCode第[42]题(Java):Trapping Rain Water

题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In

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

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

[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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是

Multiply Strings leetcode 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. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno

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

&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