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(String num1, String num2) {
		int num1Length = num1.length();
		int num2Length = num2.length();
		int d1, d2;
		int carry = 0;
		int temp;
		int[] caculate = new int[num1Length + num2Length];
		StringBuilder sb = new StringBuilder();

		for (int i = num1.length() - 1; i >= 0; i--) {
			d1 = num1.charAt(i) - ‘0‘;
			for (int j = num2.length() - 1; j >= 0; j--) {
				d2 = num2.charAt(j) - ‘0‘;
				caculate[i + j + 1] += d1 * d2;
			}
		}

		for (int i = caculate.length - 1; i >= 0; i--) {
			temp = (caculate[i] + carry) % 10;
			carry = (caculate[i] + carry) / 10;
			caculate[i] = temp;
		}

		for (int num : caculate)
			sb.append(num);

		while (sb.length() > 1 && sb.charAt(0) == ‘0‘) {
				sb.deleteCharAt(0);
		}

		return sb.toString();

	}
}

  

时间: 2024-10-27 08:33:11

Java [Leetcode 43]Multiply Strings的相关文章

[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

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&

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 =

<LeetCode OJ> 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 利用竖式的思想,

Java for LeetCode 043 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. 解题思路一: BigInteger!!! JAVA实现如下: static public String multiply(String num1, String num2) { java

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