LeetCode——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.

原题链接:https://oj.leetcode.com/problems/multiply-strings/

按照乘法原理,从个位开始一位一位相乘相加。

public class MultiplyStrings {
	public static void main(String[] args) {
		String ret = new MultiplyStrings().multiply("12","12");
		System.out.println(ret);
	}
	public String multiply(String num1, String num2) {
		if(num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0)
			return "";
		if(num1.charAt(0) == '0' || num2.charAt(0) == '0')
			return "0";
		int num = 0;
		StringBuilder builder = new StringBuilder();
		for(int i=num1.length() + num2.length();i>0;i--){
			for(int j=Math.min(i-1,num1.length());j>0;j--){
				if(i - j <= num2.length())
					num += (int)(num1.charAt(j-1)-'0') * (int)(num2.charAt(i-1-j)-'0');
			}
			if(i != 1 || num >0)
				builder.append(num%10);
			num /= 10;
		}
		return builder.reverse().toString();
	}
}
时间: 2024-10-29 23:50:50

LeetCode——Multiply Strings的相关文章

LeetCode: Multiply Strings [042]

[题目] 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. [题意] 给定用字符串表示的整数,返回两个数的乘积结果字符串.两个数字都非负,且能任意大. [思路] 1. 考虑其中一个数是0的情况 2. 模拟乘法运算过程 维护一个vecto

[leetcode]Multiply Strings @ Python

原题地址:https://oj.leetcode.com/problems/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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是

leetcode Multiply Strings

题目连接 https://leetcode.com/problems/multiply-strings/ Multiply Strings Description 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. 普通写法: class

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

[LeetCode] 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. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

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刷题笔记】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. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

Multiply Strings leetcode 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. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno

LeetCode 43. 字符串相乘(Multiply Strings)

43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. LeetCode43. Multiply Strings中等 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例?2: 输入: num1 = "123", num2 = "456&q