题目:

参考解法:

法一:

This is the standard manual multiplication algorithm. We use two nested for loops, working backward from the end of each input number. We pre-allocate our result and accumulate our partial result in there. One special case to note is when our carry requires us to write to our sum string outside of our for loop.

At the end, we trim any leading zeros, or return 0 if we computed nothing but zeros.

string multiply(string num1, string num2) {string sum(num1.size() + num2.size(), '0');for (int i = num1.size() - 1; 0 <= i; --i) {int carry = 0;for (int j = num2.size() - 1; 0 <= j; --j) {int tmp = (sum[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry;sum[i + j + 1] = tmp % 10 + '0';carry = tmp / 10;}sum[i] += carry;}size_t startpos = sum.find_first_not_of("0");if (string::npos != startpos) {return sum.substr(startpos);}return "0";
}

法二:The key part is to use a vector to store all digits REVERSELY. after the calculation, find the rightmost NON-Zero digits and convert it to a string.

class Solution {
public:string multiply(string num1, string num2) {unsigned int l1=num1.size(),l2=num2.size();if (l1==0||l2==0) return "0";vector<int> v(l1+l2,0);for (unsigned int i=0;i<l1;i++){int carry=0;int n1=(int)(num1[l1-i-1]-'0');//Calculate from rightmost to leftfor (unsigned int j=0;j<l2;j++){int n2=(num2[l2-j-1]-'0');//Calculate from rightmost to leftint sum=n1*n2+v[i+j]+carry;carry=sum/10;v[i+j]=sum%10;}if (carry>0)v[i+l2]+=carry;}int start=l1+l2-1;while(v[start]==0) start--;if (start==-1) return "0";string s="";for (int i=start;i>=0;i--)s+=(char)(v[i]+'0');return s;}
};

分析:

两个数相乘,最后的结果的长度,最大为两个乘数的长度之和。

LeetCode43——Multiply Strings(两个字符串表示的整数相乘)???相关推荐

  1. python字符串能减吗_在python中减去两个字符串(Subtract two strings in python)

    在python中减去两个字符串(Subtract two strings in python) 我应该计算两个不同列表的元素之间的差异. 这是我的代码: import operator a = ['5 ...

  2. leetcode 712. Minimum ASCII Delete Sum for Two Strings | 712. 两个字符串的最小ASCII删除和(暴力递归->傻缓存->DP)

    题目 https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 题解 经典的 暴力递归 -> 傻缓存 -&g ...

  3. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  4. php俩个字符串合并,php分割合并两个字符串的函数实例

    本文实例讲述了php分割合并两个字符串的函数..具体实现方法如下: 这里实现把两个字符串进行分割合并,例如str1=aaaa,str2=bbbb,合并后生成abababab /** * Merges ...

  5. 删除两个字符串中的特定字符使其相等,统计删除的字符的ASCII和

    为什么80%的码农都做不了架构师?>>>    Minimum ASCII Delete Sum for Two Strings 问题: Given two strings s1, ...

  6. php 字符串分割成两段,php 两个字符串分割合并的简单示例

    搜索热词 把两个字符串进行分割合并,例如str1=aaaa,str2=bbbb,合并后生成abababab,PHP分割合并两个字符串的函数,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来 ...

  7. 制作两个字符串字谜的最小步骤数

    Prerequisite: 先决条件: Hashing data structure 散列数据结构 Problem statement: 问题陈述: Find the minimum number o ...

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

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

  9. 查找两个字符串中相同字符串_使两个字符串相同的最低成本

    查找两个字符串中相同字符串 Problem statement: 问题陈述: Given two strings string1 and string2 find the minimum cost r ...

最新文章

  1. Comparator 和 Comparable
  2. 仓库管理常见问题及价值
  3. Teamviewer 远程ssh命令行更改密码启动
  4. Android 10.0 PackageManagerService(一)工作原理及启动流程-[Android取经之路]
  5. Keil工程Lib库文件的制作和运用
  6. STM32 定时器 定时时间的计算
  7. 从数据结构到算法:图网络方法初探
  8. 登陆窗体相关的控件 1124
  9. win10 + Terminal + WSL+ oh-my-zsh 配置漂亮实用的windows终端及配置ubuntu不加.exe打开windows程序
  10. 【registry】Reader schema missing default value for field: age
  11. 遍历map时删除不需要的元素方法
  12. 1941套站点模版,终生收藏,个个精品
  13. 流量治理神器-Sentinel限流熔断应用实战
  14. sdelete使用指南(Windows Sysinternals Suite工具介绍转)
  15. ST语言入门(维修电工1)
  16. 计算机桌面图片怎么设置大小,电脑桌面的图标大小怎么调整?
  17. MySQL 怎么插入10天前的日期_使用 MySQL 的 SQL_MODE 有哪些坑,你知道么?
  18. scikit-learn入门到精通(二):seting和estimator
  19. 疑似网易泄露用户密码事件浅析
  20. 应聘中巧用积极的心理暗示

热门文章

  1. [cerc2012][Gym100624B]20181013
  2. Andorid获取状态栏高度
  3. 转载Linq中GroupBy方法的使用总结
  4. springside 4 web-init 启动
  5. Apache Accumulo 1.9.3 发布,高性能 K/V 存储方案
  6. PHP 完整实战23种设计模式
  7. 国产毫米波雷达领域的领头羊,木牛科技将在明年量产77GHz汽车雷达
  8. 阿里云胡晓明:数据智能将为城市生活带来真正价值
  9. IBM 安全部门 CTO:AI 必须被重新定义为“增强智能”
  10. 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸