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 Solution {
public:
	string multiply(string num1, string num2) {
		int n = num1.size(), m = num2.size();
		if (num1 == "0" || num2 == "0") return "0";
		ans = "";
		int len = n + m + 10;
		ret = new int[len];
		memset(ret, 0, sizeof(int)* (len));
		reverse(num1.begin(), num1.end());
		reverse(num2.begin(), num2.end());
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				ret[i + j] += (num1[i] - ‘0‘) * (num2[j] - ‘0‘);
			}
		}
		for (int i = 0; i < len - 1; i++) {
			if (ret[i] > 9) {
				ret[i + 1] += ret[i] / 10;
				ret[i] %= 10;
			}
		}
		int j = len - 1;
		while (!ret[j]) j--;
		for (int i = j; ~i; i--) ans += (char)(ret[i] + ‘0‘);
		delete []ret;
		return ans == "" ? "0" : ans;
	}
private:
	string ans;
	int *ret, len;
};

文艺写法:

class Solution(object):
    def multiply(self, num1, num2):
    	return str(int(num1) * int(num2))

2B写法:

class Complex {
public:
	Complex(double _r_ = 0.0, double _i_ = 0.0) :r(_r_), i(_i_) {}
	~Complex() {}
	inline Complex operator+(const Complex &x) const{
		return Complex(r + x.r, i + x.i);
	}
	inline Complex operator-(const Complex &x) const{
		return Complex(r - x.r, i - x.i);
	}
	inline Complex operator*(const Complex &x) const{
		return Complex(r * x.r - i * x.i, r * x.i + i * x.r);
	}
public:
	double r, i;
};
class FastFourierTransform {
public:
	FastFourierTransform() = default;
	FastFourierTransform(const string s1, const string s2) {
		int i = 0;
		ans = "";
		N = 1, len1 = s1.length(), len2 = s2.length();
		while (N < len1 << 1 || N < len2 << 1) N <<= 1;
		x1 = new Complex[N + 1];
		x2 = new Complex[N + 1];
		for (i = 0; i < len1; i++) {
			x1[i].r = s1[len1 - i - 1] - ‘0‘;
			x1[i].i = 0.0;
		}
		for (; i < N; i++) x1[i].r = x1[i].i = 0.0;
		for (i = 0; i < len2; i++) {
			x2[i].r = s2[len2 - i - 1] - ‘0‘;
			x2[i].i = 0.0;
		}
		for (; i < N; i++) x2[i].r = x2[i].i = 0.0;
	}
	~FastFourierTransform() { delete[]x1; delete[]x2; delete[]ret; }
	inline string work() {
		int i = 0;
		ret = new int[N + 1];
		memset(ret, 0, sizeof(int)* (N + 1));
		fft(x1, N, 1);
		fft(x2, N, 1);
		for (i = 0; i < N; i++) x1[i] = x1[i] * x2[i];
		fft(x1, N, -1);
		for (i = 0; i < N; i++) ret[i] = (int)(x1[i].r + 0.5);
		for (i = 0; i < N; i++) {
			ret[i + 1] += ret[i] / 10;
			ret[i] %= 10;
		}
		i = len1 + len2 - 1;
		while (ret[i] <= 0 && i > 0) i--;
		for (; ~i; i--) ans += (char)(ret[i] + ‘0‘);
		return ans;
	}
private:
	string ans;
	Complex *x1, *x2;
	int N, len1, len2, *ret, *vis;
	const double Pi = acos(-1.0);
	inline void brc(Complex *y, int N) {
		int i, j, k, ret = N >> 1;
		for (i = 1, j = N >> 1; i < N - 1; i++) {
			if (i < j)    swap(y[i], y[j]);
			k = ret;
			while (j >= k) {
				j -= k;
				k >>= 1;
			}
			if (j < k) j += k;
		}
	}
	inline void fft(Complex *&y, int N, double on) {
		int i, j, h, k;
		Complex u, t;
		brc(y, N);
		for (h = 2; h <= N; h <<= 1) {
			Complex wn(cos(on * 2 * Pi / h), sin(on * 2 * Pi / h));
			for (j = 0; j < N; j += h) {
				Complex w(1, 0);
				for (k = j; k < j + h / 2; k++) {
					u = y[k];
					t = w * y[k + h / 2];
					y[k] = u + t;
					y[k + h / 2] = u - t;
					w = w * wn;
				}
			}
		}
		if (-1 == on) for (i = 0; i < N; i++) y[i].r /= N;
	}
};
class Solution {
public:
	string multiply(string num1, string num2) {
		if (num1 == "0" || num2 == "0") return "0";
		ret = new FastFourierTransform(num1, num2);
		ans = ret->work();
		delete ret;
		return ans;
	}
private:
	string ans;
	FastFourierTransform *ret;
};
时间: 2024-10-09 03:10:29

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 解题报告

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. 原题链接:https://oj.leetcode.com/problems/multiply-strings/ 按照乘法原理,从个位开始一位一位相乘相加. public class Mu

[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