Leetcode43. 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.

个人思路

这个题目是写一个不限定长度的乘法器,花了三个晚上才AC。

我的思路是1、将乘数中的每个数字依次读出,与被乘数的每一位相乘,使用base存放值,add存放进位值。将求得的结果用二元数组vector<vector<int>>保存。

     2、将二元数组中的元素前后均对齐,无法对其的补0占位。例如,123*456我们得到 738 615 492。我们采用前后补0的方式得到 00738,06150,49200.

       然后同样使用base和add两个变量将其求和。结果保存在数组中,最后讲数组转化成string形式。

注意:乘数中存在0时,要特殊考虑,因为返回结果只能为0,不能为0000。

这种方法时间开销比较大,坚持写完也是因为自己花了很多时间调试。

 1 class Solution {
 2 public:
 3     string multiply(string num1, string num2) {
 4         if(num1=="0"||num2=="0"){
 5             return "0";
 6         }
 7         vector< vector<int> > result;
 8          //string answer;
 9          //向上进位的值
10          int add = 0;
11          //留在该位的值
12          int base;
13          //两数相乘的结果
14          long multi;
15          long sumsult = 0;
16         // num1 = num1.crbegin();
17         // num2 = num2.crbegin();
18          for(int j = num2.size()-1;j>=0;j--){
19              vector<int> lineresult;
20              for(int i = num1.size()-1;i>=0;i--){
21                 multi = atoi((num1.substr(i,1)).c_str())*atoi((num2.substr(j,1)).c_str());
22                 base = (multi+add)%10;
23                 lineresult.push_back(base);
24                 add = (multi+add)/10;
25             }
26             if(add!=0){
27                 lineresult.push_back(add);
28                 add = 0;
29             }
30             result.push_back(lineresult);
31          }
32          //将所有项的位数对其
33          int lineNum = result.size();
34          for(int i = 0;i<lineNum;i++){
35              for(int j = 0;j<i;j++){
36                 result[i].insert(result[i].begin(),0);
37              }
38          }
39          for(int i = 0;i<lineNum;i++){
40              for(int k = (result[i]).size();k<(result[lineNum-1]).size();k++){
41                  result[i].push_back(0);
42              }
43          }
44          add = 0;
45          base = 0;
46          //i代表每一位数,j代表加数的个数
47           for(int i = 0;i<result[lineNum-1].size();i++){
48              int addsult = add;
49              for(int j = 0;j<lineNum;j++){
50                  addsult +=result[j][i];
51              }
52              base = addsult%10;
53              result[lineNum-1][i] = base;
54              add = addsult/10;
55          }
56          if(add!=0){
57              result[lineNum-1].push_back(add);
58              add = 0;
59          }
60
61          string turn = "";
62          for (int i = result[lineNum-1].size()-1; i >= 0; i--) {
63             turn  += to_string(result[lineNum-1][i]);
64         }
65
66          return turn;
67
68     }
69 };

他山之石

这个题目是写一个不限定长度的乘法器,花了三个晚上才AC。

时间: 2025-01-06 22:07:04

Leetcode43. 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

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转换

&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

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 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: 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 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 利用竖式的思想,

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