43. Multiply Strings java solutions

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 use internal library such as BigInteger.
 1 public class Solution {
 2     public String multiply(String num1, String num2) {
 3         StringBuilder ans = new StringBuilder();
 4         int[] tmp = new int[num1.length() + num2.length()];
 5         for(int i = num1.length()-1; i>=0;i--){
 6             for(int j = num2.length()-1; j>=0;j--){
 7                 tmp[i+j+1] += (num1.charAt(i) - ‘0‘)*(num2.charAt(j) - ‘0‘);
 8             }
 9         }
10
11         for(int i = tmp.length-1; i >= 1;i--){
12             tmp[i-1] += tmp[i]/10;
13             tmp[i] %= 10;
14         }
15
16         int left = 0;
17         while(left < tmp.length-1 && tmp[left] == 0)left++;// 这里需要考虑一个 num1 和 num2 有一个为0 的情况。
18         for(;left < tmp.length; left++){
19             ans.append(tmp[left]);
20         }
21         return ans.toString();
22     }
23 }

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

43. Multiply Strings java solutions的相关文章

&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

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

43. Multiply Strings 字符串相乘

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading z

【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

[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

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 multiply(string& num, char ch){ int n = ch - '0'; string s; int carry = 0; int x; for(

LeetCode: Multiply Strings. 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. public class Solution { //模拟手算乘法 public String multiply(String num1, String num2) { int n = n

【一天一道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