[leedcode 43] Multiply Strings

Straight forward idea. Just like the way we multiply numbers. Don‘t forget considering the carry and be careful. e.g.

123*456,
what we usually do is:
      123
*    456
-----------
      738
    615
+492
-----------
  56088
thus, 123*456 = 56088.
In the same way, the algorithm is:

from end to start position, use a new array to store temproary digit.
A*B
(1)For each element B[i]
    Compute tmp = B[i]*A
    Add tmp to the previous result, note the start position. res = res"+"tmp
(2)Return result.

To be specific,
(1) char2int,     int(char-‘0‘);
(2) int2char,     char(int+‘0‘)
(3) Don‘t forget the carry in each add or multiply operation.
(4) Don‘t forget the carry after last operation. e.g.  82+33 = 115.
(5) Be careful with the string order and the number order.

public class Solution {
    public String multiply(String num1, String num2) {
        //用字符串模拟乘法,注意事项见上面分析。注意result的第0位是结果的个位

        int len1=num1.length();
        int len2=num2.length();
        int[] result=new int[len1+len2];
        int carry=0;
        int i=0;
        int j=0;
        for( i=0;i<len2;i++){
            int n2=num2.charAt(len2-1-i)-‘0‘;
            carry=0;
            for(j=0;j<len1;j++){
                result[i+j]+=n2*(num1.charAt(len1-1-j)-‘0‘)+carry;

                carry=result[i+j]/10;
                result[i+j]%=10;
            }
            result[i+j]=result[i+j]+carry;
        }
        i=len1+len2-1;
        while(i>0){
            if(result[i]!=0)
                break;
            i--;
        }
        StringBuilder res=new StringBuilder();
        int k=i;
        while(k>=0){
            res.append((char)(result[k]+‘0‘));//res.append(result[k]+‘0‘);会得整数,比如result[k]为0时,添加的是48
            k--;
        }

        return res.toString();
    }
}
时间: 2024-12-28 18:15:25

[leedcode 43] Multiply Strings的相关文章

&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

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

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题意分析&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(

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 su

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