19.2.4 [LeetCode 43] Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

 1 class Solution {
 2 public:
 3     string multiply(string num1, string num2) {
 4         reverse(num1.begin(), num1.end());
 5         reverse(num2.begin(), num2.end());
 6         int nn[250] = { 0 }, maxl = 0;
 7         int l1 = num1.length(), l2 = num2.length();
 8         for(int i=0;i<l1;i++)
 9             for (int j = 0; j < l2; j++) {
10                 nn[i + j] += (num1[i] - ‘0‘)*(num2[j] - ‘0‘);
11                 int tmp = i + j;
12                 while (nn[tmp] > 9) {
13                     nn[tmp + 1] += nn[tmp] / 10;
14                     nn[tmp] %= 10;
15                     tmp++;
16                 }
17                 if (nn[tmp] == 0)continue;
18                 maxl = max(tmp, maxl);
19             }
20         string ans = "";
21         for (int i = maxl; i >= 0; i--) {
22             char ch = nn[i] + ‘0‘;
23             ans += ch;
24         }
25         return ans;
26     }
27 };

大整数乘法

原文地址:https://www.cnblogs.com/yalphait/p/10351729.html

时间: 2024-10-07 23:57:57

19.2.4 [LeetCode 43] Multiply Strings的相关文章

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 43 Multiply Strings 大数相乘

感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){ return "0"; } int len1 = num1.length(); int len2 = num2.length(); int len = len1+len2; int[] arr =

&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

43. Multiply Strings(js)

43. Multiply Strings Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "6" Example 2: Inp

【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】#43. Multiply Strings

一天一道LeetCode系列 (一)题目 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. Converting the input string to integer is NOT allowed. You should NOT us

【LeetCode题意分析&amp;解答】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表示的非负大数的乘积,乘数可以是任意大小. 解答: 可以用一个临时List表示乘积的每一位,然后对两个乘数每一位两两相乘,并将结果填到相应的List