博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Multiply Strings
阅读量:5343 次
发布时间:2019-06-15

本文共 2712 字,大约阅读时间需要 9 分钟。

2013.12.15 04:07

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.

Solution:

  I just did a digit-by-digit multiplication. There seemed to be a very complicated O(n * log(n)) solution, using Fast Fourier Transformation.

  Here is the link to that solution, if you are eager to dip into it: 

  Also there is an exercise problem on "", I'll paste my O(n * log(n)) solution here when I understand how it works (^_^)

  Time complexity is O(n^2), where n is the length of the array. Space complexity is O(n), since storing the array requires O(n) space.

Accepted code:

1 // 1CE, 1AC, nice performance 2 class Solution { 3 public: 4     string multiply(string num1, string num2) { 5         // IMPORTANT: Please reset any member data you declared, as 6         // the same Solution instance will be reused for each test case. 7         if(num1 == "0" || num2 == "0"){ 8             return "0"; 9         }10         11         int *a1, *a2, *a3;12         int max_len, n;13         char *buf;14 15         max_len = num1.length() + num2.length() + 5;16         a1 = new int[max_len];17         a2 = new int[max_len];18         a3 = new int[max_len];19         buf = new char[max_len];20         // 1CE here, declaration for $j missing21         int i, j;22         23         for(i = 0; i < max_len; ++i){24             a1[i] = a2[i] = a3[i] = 0;25             buf[i] = 0;26         }27         28         n = num1.length();29         for(i = 0; i <= n - 1; ++i){30             a1[n - 1 - i] = num1[i] - '0';31         }32         33         n = num2.length();34         for(i = 0; i <= n - 1; ++i){35             a2[n - 1 - i] = num2[i] - '0';36         }37         38         for(i = 0; i < max_len; ++i){39             if(a1[i] == 0){40                 continue;41             }42             for(j = 0; i + j < max_len; ++j){43                 a3[i + j] += a1[i] * a2[j];44             }45         }46         47         for(i = 0; i < max_len - 1; ++i){48             a3[i + 1] += a3[i] / 10;49             a3[i] %= 10;50         }51         a3[max_len - 1] %= 10;52         i = max_len - 1;53         while(i >=0 && a3[i] == 0){54             --i;55         }56         57         if(i < 0){58             sprintf(buf, "0");59         }else{60             j = 0;61             while(i >= 0){62                 buf[j++] = a3[i--] + '0';63             }64         }65         66         string res = string(buf);67         68         delete[] a1;69         delete[] a2;70         delete[] a3;71         delete[] buf;72         return res;73     }74 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3474949.html

你可能感兴趣的文章
List<string> 去重复 并且出现次数最多的排前面
查看>>
js日志管理-log4javascript学习小结
查看>>
Android之布局androidmanifest.xml 资源清单 概述
查看>>
How to Find Research Problems
查看>>
Linux用户管理
查看>>
数据库第1,2,3范式学习
查看>>
《Linux内核设计与实现》第四章学习笔记
查看>>
使用iperf测试网络性能
查看>>
struts2入门之准备工作
查看>>
从C语言的弱类型属性说起
查看>>
图片的显示隐藏(两张图片,默认的时候显示第一张,点击的时候显示另一张)...
查看>>
Docker 安装MySQL5.7(三)
查看>>
python 模块 来了 (调包侠 修炼手册一)
查看>>
关于CSS的使用方式
查看>>
本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
查看>>
跨域解决方案之CORS
查看>>
学习RESTFul架构
查看>>
分析语句执行步骤并对排出耗时比较多的语句
查看>>
原生JS轮播-各种效果的极简实现
查看>>
软件工程总结作业---提问回顾与个人总结
查看>>