Leetcode:multiply Strings

Source: Internet
Author: User
Tags multiply strings

Problem:

Given numbers represented as strings, return multiplication of the numbers as a string.

Note:the numbers can be arbitrarily large and is non-negative.



Solution:

Multiply each bit of string 1 with string2. Be careful of the carryin and the position.



Code:

public class Solution {public String multiply (string num1, String num2) {if (NUM1 = null | | num2 = NULL) RE        Turn "";        Int[] result = new Int[num1.length () + num2.length ()];        int carryin = 0;            for (int i = Num1.length ()-1; I >= 0; i--) {carryin = 0;                for (int j = num2.length ()-1; J >= 0; j--) {int a = Num1.charat (i)-' 0 ';                int B = Num2.charat (j)-' 0 ';                int TMP = A * b + carryin + result[num1.length ()-1 + num2.length ()-1-i-J];                Carryin = TMP/10;            Result[num1.length ()-1 + num2.length ()-1-i-j] = tmp% 10;        } result[num1.length ()-1 + num2.length ()-1-i + 1] = Carryin;        }//Result[num1.length () + num2.length ()-1] = Carryin;        int i = num1.length () + num2.length ()-1;        while (I >= 1 && result[i] = = 0) {i--;        } String s = ""; for (int j = i; J >= 0;        j--) {s = s + result[j];    } return s; }}


Leetcode:multiply Strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.