leetcode 43. 字符串相乘 高精度乘法模板

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

示例 1:

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

示例 2:

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

说明:

num1 和 num2 的长度小于110。
num1 和 num2 只包含数字 0-9。
num1 和 num2 均不以零开头,除非是数字 0 本身。
不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/multiply-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 官方题解用快速傅里叶变换优化到了难以置信的复杂度

大数乘法模板

//O(n*m)
string multiply(string A, string B) {
  if(A == "0" || B == "0") return "0";
   int n = A.length(), m = B.length();
   vector<int> s(n+m, 0);
   string ans;
   for(int i=n-1; i>=0; i--) {
       int x = A[i] - '0', y;
       for(int j=m-1; j>=0; j--) {
           y = B[j] - '0';
           s[i+j+1] += x * y;
       }
   }
   for(int i=m+n-1; i>0; i--)  {
       s[i-1] += s[i]/10;
       s[i] %= 10;
   }
   int i = s[0] == 0 ? 1 : 0;
   for( ; i<n+m; i++) ans.push_back(s[i]+'0');
   return ans;
}

#ifdef debug
#include <time.h>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "\033[31;1m " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

class Solution {
public:
    string multiply(string A, string B) {
        if(A == "0" || B == "0") return "0";
        int n = A.length(), m = B.length();
        vector<int> s(n+m, 0);
        string ans;
        for(int i=n-1; i>=0; i--) {
            int x = A[i] - '0', y;
            for(int j=m-1; j>=0; j--) {
                y = B[j] - '0';
                s[i+j+1] += x * y;
            }
        }
        for(int i=m+n-1; i>0; i--)  {
            s[i-1] += s[i]/10;
            s[i] %= 10;
        }
        int i = s[0] == 0 ? 1 : 0;
        for( ; i<n+m; i++) ans.push_back(s[i]+'0');
        return ans;
    }
};

#ifdef debug
signed main() {
    string A = "789789", B = "78978";
    Solution s;
    cout << s.multiply(A, B) << endl;





   return 0;
}
#endif 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值