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;
};
posted @ 2015-12-11 20:26  GadyPu  阅读(167)  评论(0编辑  收藏  举报