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://leetcodenotes.wordpress.com/2013/10/20/leetcode-multiply-strings-%E5%A4%A7%E6%95%B4%E6%95%B0%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B9%98%E6%B3%95/comment-page-1/#comment-122),非常精巧的写法:

这个题第二遍做code就写的挺完美的,高兴~~几个要点:

  • 直接乘会溢出,所以每次都要两个single digit相乘,最大81,不会溢出。
  • 比如385 * 97, 就是个位=5 * 7,十位=8 * 7 + 5 * 9 ,百位=3 * 7 + 8 * 9 …
    可以每一位用一个Int表示,存在一个int[]里面。
  • 这个数组最大长度是num1.len + num2.len,比如99 * 99,最大不会超过10000,所以4位就够了。
  • 这种个位在后面的,不好做(10的0次方,可惜对应位的数组index不是0而是n-1),
    所以干脆先把string reverse了代码就清晰好多。
  • 最后结果前面的0要清掉。

代码如下:

1     num1 = new StringBuilder(num1).reverse().toString();
 2     num2 = new StringBuilder(num2).reverse().toString();
 3     // even 99 * 99 is < 10000, so maximaly 4 digits
 4     int[] d = new int[num1.length() + num2.length()];
 5     for (int i = 0; i < num1.length(); i++) {
 6         int a = num1.charAt(i) - ‘0‘;
 7         for (int j = 0; j < num2.length(); j++) {
 8             int b = num2.charAt(j) - ‘0‘;
 9             d[i + j] += a * b;
10         }
11     }
12     StringBuilder sb = new StringBuilder();
13     for (int i = 0; i < d.length; i++) {
14         int digit = d[i] % 10;
15         int carry = d[i] / 10;
16         sb.insert(0, digit);
17         if (i < d.length - 1)
18             d[i + 1] += carry;
19         }
20     //trim starting zeros
21     while (sb.length() > 0 && sb.charAt(0) == ‘0‘) {
22         sb.deleteCharAt(0);
23     }
24     return sb.length() == 0 ? "0" : sb.toString();
25 }

Multiply Strings leetcode java

时间: 2024-08-04 19:28:19

Multiply Strings leetcode java的相关文章

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. Subscribe to see which companies asked this question ACM竞赛常考的大数相乘算法,利用字符串来表示大数字进行计算. string m

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

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

&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

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位,则