LeetCode 43. Multiply Strings

Solution1:我的答案
作为一个hard题,提交一次就过真是让我hin开心啊!!!
就是方法有点笨。。

class Solution {
public:string multiply(string num1, string num2) {if (num1 == "0" || num2 == "0") return "0";string res = "";vector<string> temp_res;int max_size = 0, i = 0, carry = 0;for (int i = num2.size() - 1; i >= 0; i--) {string temp_str = my_multi(num1, num2[i]);string space(num2.size() - 1 - i, ' ');temp_str = temp_str + space;max_size = max(max_size, (int)temp_str.size());temp_res.push_back(temp_str);}while (i < max_size) { //i的最大索引值为max_size-1int temp_sum = 0;for (int j = 0; j < temp_res.size(); j++) { //遍历所有的加数if (i <= temp_res[j].size() - 1) {int index = temp_res[j].size() - 1 - i;if (temp_res[j][index] == ' ') continue;else temp_sum += temp_res[j][index] - '0';}}res = to_string((carry + temp_sum) % 10) + res;carry = (carry + temp_sum) / 10;i++;}if (carry)res = to_string(carry) + res;return res;}// 子函数为多位数和1位数相乘string my_multi(string &num1, char num2) {string res = num1;int i = num1.size() - 1, temp1 = 0, temp_sum = 0, carry = 0;for (; i >= 0; i--) {temp_sum = (num1[i] - '0') * (num2 - '0');res[i] = (carry + temp_sum) % 10 + '0';carry = (carry + temp_sum) / 10;}if (carry)res = to_string(carry) + res;return res;}
};

Solution2:花花酱
参考网址:https://zxi.mytechroad.com/blog/simulation/leetcode-43-multiply-strings/
花花酱真牛逼啊。。。
这种方法不知道好到哪里去了~~~
Time complexity: O(l1∗l2)O(l1∗l2)O(l1*l2)
Space complexity: O(l1+l2)O(l1+l2)O(l1 + l2)
说明:两个位数分别是m,n的整数相乘,则乘积最多有m+n位。
这么理解:比如m=3,n=2时最大乘积是999*99,考虑1000*100 = 100000,前者乘积的位数肯定小于后者,而100000有6位数,100000-1 = 99999,有5位数,所以前者乘积最多是5位数,即m+n

// Author: Huahua
// Running time: 4 ms
class Solution {
public:string multiply(string num1, string num2) {const int l1 = num1.length();const int l2 = num2.length();string ans(l1 + l2, '0');for (int i = l1 - 1; i >= 0; --i)for (int j = l2 - 1; j >= 0; --j) {//这个算法中很核心的一点:就是乘数中的i,j位的乘积对应积中的第(i+j+1)位数字int sum = (ans[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0');        ans[i + j + 1] = (sum % 10) + '0';ans[i + j] += sum / 10;}for (int i = 0; i < ans.length(); ++i)// if()中第二个条件是处理至少一个数为0的情况if (ans[i] != '0' || i == ans.length() - 1) return ans.substr(i);return "";}
};

【大数相乘】LeetCode 43. Multiply Strings相关推荐

  1. leetcode 43. Multiply Strings | 43. 字符串相乘(Java)

    题目 https://leetcode.com/problems/multiply-strings/ 题解 模拟手动乘法,列竖式 class Solution {public String multi ...

  2. LeetCode 43. Multiply Strings (竖式乘法)C++

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  3. 43. Multiply Strings 字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  4. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. LeetCode刷题实战(43):Multiply Strings

    题目描述: 43 Multiply Strings 28.7% Medium Given two non-negative integers num1 and num2 represented as ...

  6. php大数相乘,简单的大数相乘算法

    大数相乘最直接的算法就是模拟小学学到的竖式乘法,可以使用数组或者字符串来存储乘数和被乘数,php代码实现如下: /** * 大数相乘代码 */ function multiply($str1,$str ...

  7. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  8. LeetCode-Problem 43:大数相乘

    算法问题 给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积. 算法实现 以下是大神的算法,膜拜大神: 首先,长度位m的数乘以长度为n的数的结果不超过m+n. ...

  9. 【LeetCode43:字符串相乘(大数相乘)(Java实现)】

    字符串相乘(大数相乘) 一.题目描述 1.题目内容 2.样例 二.解决方案 1.算法流程 1)分析(竖式计算) 2)算法流程 2.Java实现 1)核心代码 2)完整测试代码 一.题目描述 1.题目内 ...

最新文章

  1. 一图看懂《中关村人工智能产业培育行动计划》
  2. 【并发编程】Atomic与CAS
  3. Android IOS WebRTC 音视频开发总结(三三)-- Periscope介绍
  4. Java编程思想学习(一) 一切都是对象
  5. php openssl做什么,php开启openssl的方法
  6. mysql获取数据库名_mysql获取数据库名
  7. 张朝阳:选校草、跑马拉松、开5G峰会 搜狐最近要干这些事
  8. 《深入理解Java虚拟机》第6章 类文件结构
  9. Windows7与Window2008 64位IIS7上面配置操作Excel
  10. 整理iOS9适配中出现的坑(图文)
  11. Python函数嵌套
  12. ENVI用ROI进行裁剪
  13. DDoS高防云服务器如何防御攻击
  14. atomic 原子操作
  15. TypeError: _typeof4 is not a function,解决微信小程序报错
  16. 控制台基于Quartz.Net组件实现定时任务调度(一)
  17. Android选择本地视频和照片上传到服务器
  18. Resco MobileForms Toolkit 2010的破解
  19. 产品运营数据分析—SPSS数据分组案例
  20. GPS信号接收机的频偏和相位锁定matlab仿真

热门文章

  1. 【慢慢学算法】:qsort()与sort的用法(收藏)
  2. (一)Linux下C++ OpenCV开发环境搭建
  3. python 多线程 异步_python 多线程异步
  4. 在线运行java代码并得到结果_Java代码是如何运行的?
  5. time函数python_python time模块函数
  6. 在线计算机 授课,在线计算机教育网站计蒜客改版 推出算法竞赛课程
  7. 实际返回的行数超出请求的行数_代码行数越少越好?
  8. java io教程_Java IO教程
  9. powermock私有字段_使用PowerMock的EasyMock私有方法模拟
  10. jsf如何与数据库连接_JSF身份验证登录注销数据库示例