【LeetCode】#43字符串相乘(Multiply Strings)

【LeetCode】#43字符串相乘(Multiply Strings)

题目描述

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

示例

示例 1:

输入: num1 = “2”, num2 = “3”
输出: “6”

示例 2:

输入: num1 = “123”, num2 = “456”
输出: “56088”

Description

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example

Example 1:

Input: num1 = “2”, num2 = “3”
Output: “6”

Example 2:

Input: num1 = “123”, num2 = “456”
Output: “56088”

解法

class Solution{
	public String multiply(String num1, String num2){
		char[] value = new char[num1.length()+num2.length()];
		for(int i=num1.length()-1; i>=0; i--){
			for(int j=num2.length()-1; j>=0; j--){
				value[i+j+1] += (num1.charAt(i)-'0')*(num2.charAt(i)-'0');
			}
		}
		int carry = 0;
		for(int i=value.length-1; i>=0; i--){
			value[i] += carry;
			carry = value[i]/10;
			value[i] %= 10;
		}
		int beginIndex = 0;
		while(beginIndex<value.length-1 && value[beginIndex]==0){
			beginIndex++;
		}
		for(int i=beginIndex; i<value.length; i++){
			value[i] += '0';
		}
		return new String(value, beginIndex, value.length-beginIndex);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43858604/article/details/84844123