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

利用竖式的思想,进行乘法运算。

3    4

*      1    3

——————

9      12

3     4

——————

3     13     12

再处理进位:

3 13 12 - > 3 14 2 -> 4 4 2

代码如下:

class Solution {
public:
    string multiply(string num1, string num2) {

        int flag = 1;

        //先处理符号
        if (num1[0] == ‘-‘ || num2[0] == ‘-‘) {
            if (num1[0] == ‘-‘) {
                flag *= -1;
                num1 = num1.substr(1, num1.size() - 1);
            }
            if (num2[0] == ‘-‘) {
                flag *= -1;
                num2 = num2.substr(1, num2.size() - 1);
            }
        }

        int length = num1.size() + num2.size() + 1;
        int s[length];

        memset(s, 0, sizeof(int) * length);

        int i = 0, temp = 0;

        for (i = 0; i < num1.size(); i++) {
            for (int j = 0; j < num2.size(); j++) {
                s[(num1.size() - i - 1) + (num2.size() - j - 1)] += (num1[i] - ‘0‘) * (num2[j] - ‘0‘);
            }
        }

        //进位
        for (i = 0; i < length; i++) {
            s[i + 1] += s[i] / 10;
            s[i] = s[i] % 10;
        }

        for (i = 0; i < length / 2; i++) {
            temp = s[i];
            s[i] = s[length - i - 1];
            s[length - i - 1] = temp;
        }

        string ans = flag < 0 ? "-" : "";
        for (i = 0, temp = -1; i < length; i++) {
            if (s[i] != 0) {
                temp = 1;
            }
            if (temp > 0) {
                ans += s[i] + ‘0‘;
            }
        }
        return ans == "" ? "0" : ans;
    }
};
时间: 2024-10-11 05:13:18

LeetCode 043 Multiply Strings的相关文章

Java for LeetCode 043 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. 解题思路一: BigInteger!!! JAVA实现如下: static public String multiply(String num1, String num2) { java

【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 (19) 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. 对两组非负数字进行相乘,使用数组表示数字,且题目中说明数组很大,因此,因此不能直接将数组转换成数字相乘.这道题目是要求自己构造乘法的思路,需要注意的地方主要为进位的处理. 解题

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

【leetcode】Multiply Strings(middle)

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 num1, string num2) { vector<int> v1, v

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. 这道题的意思是字符串表示的数字相乘. 没啥好说的直接付代码. public String multiply(String num1, String num2) { BigInteger

[C++]LeetCode: 69 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. Answer 1: 基础法 大整数乘法 根据小学手算乘法的规则,我们把每一位相乘,得到一个没有进位的临时结果,如图中间的一行红色数字就是临时结果,然后把临时结果从低位起一次进位.

【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. [解析] 题意:两个字符串表示的非负整数相乘,用字符串的形式返回积. 思路:逐位相乘. 关键:中间结果如何保存?如果用字符串保存中间结果,频繁该值不太方便,所以还是用整数数组保