LeetCode – Refresh – Multiply Strings

Notes:

1. Dont have to allocate l1*l2, just l1+l2 is fare enough.

2. remember i--, j--.

3. upgrade[i+j] is += not = rec[i+j+1]/10;

 1 class Solution {
 2 public:
 3     string multiply(string num1, string num2) {
 4         int l1 = num1.size(), l2 = num2.size();
 5         if (l1 == 0 || l2 == 0 || num1 == "0" || num2 == "0") return "0";
 6         vector<int> rec(l1+l2, 0);
 7         unordered_map<int, int> upgrade;
 8         string result;
 9         for (int i = l1-1; i >= 0; i--) {
10             for (int j = l2-1; j >=0; j--) {
11                 rec[i+j+1] += int(num1[i] - ‘0‘) * int(num2[j] - ‘0‘);
12                 rec[i+j+1] += upgrade[i+j+1];
13                 upgrade[i+j+1] = 0;
14                 upgrade[i+j] += rec[i+j+1]/10;
15                 rec[i+j+1] %= 10;
16                 if (i == 0 && j == 0 && upgrade[0]) rec[0] = upgrade[0];
17             }
18         }
19         bool flag = true;
20         for (int i = 0; i < l1+l2; i++) {
21             if (flag && rec[i] == 0) continue;
22             else flag = false;
23             result += char(rec[i] + ‘0‘);
24         }
25         return result;
26     }
27 };
时间: 2025-01-01 11:40:43

LeetCode – Refresh – Multiply Strings的相关文章

【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 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 (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

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

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