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.
  • Converting the input string to integer is NOT allowed.
  • You should NOT use internal library such as BigInteger.

高精度乘法。需要注意的是char表示的数字和int对应的数字之间的转化。

class Solution {
public:
string multiply(string num1, string num2) {
int l1=num1.length(),l2=num2.length();
if(l1==||l2==){
string ans="";
return ans;
}
string ans(l1+l2,'');
for(int i=l1-;i>=;i--){
int add=;
for(int j=l2-;j>=;j--){
int t=(ans[i+j+]-'')+(num1[i]-'')*(num2[j]-'')+add;
ans[i+j+]=t%+'';
add=t/;
}
ans[i]=add+'';
}
size_t s0 = ans.find_first_not_of('');
if(s0!=string::npos){
return ans.substr(s0);
}
return "";
}
};

leetcode 43. Multiply Strings(高精度乘法)的更多相关文章

  1. [leetcode]43. Multiply Strings高精度乘法

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

  2. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  3. [LeetCode] 43. Multiply Strings 字符串相乘

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

  4. LeetCode(43. Multiply Strings)

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

  5. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  6. 43. Multiply Strings (大数乘法)

    DescriptionHintsSubmissionsDiscussSolution   Pick One Given two non-negative integers num1 and num2  ...

  7. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  8. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

  9. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

随机推荐

  1. 关于layui-layer独立组件--弹出层

    官方下载文档链接:http://layer.layui.com/ =================================================================== ...

  2. window.close关闭当前页面

    浏览器处于安全策略考虑,只允许Javascript关闭由javascript打开的页面,为了用js关闭当前窗口,我们可以这么考虑,这也是最常用的做法. <a href="javascr ...

  3. HDU 3461 Code Lock(并查集的应用+高速幂)

    * 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...

  4. PHP正则匹配6到16位字符组合(且只能为数字、字母、下划线)

    php正则匹配6到16位的字符串. 只允许包含数字.字母.下划线组成的6到16位字符,符合返回ture,否则返回false. 解答: 6到16位,正则可以这样写:{6,16}. 任意的字符6到16位的 ...

  5. Ubuntu 14.04下单节点Ceph安装(by quqi99)

    作者:张华  发表于:2014-06-23版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99 ) Ceph ...

  6. Java&amp;Xml教程(十)XML作为属性文件使用

    我们一般会将Java应用的配置參数保存在属性文件里.Java应用的属性文件能够是一个正常的基于key-value对,以properties为扩展名的文件.也能够是XML文件. 在本案例中.將会向大家介 ...

  7. python 基础 1.5 python数据类型(四)--字典常用方法示例

    一. 字典 #字典 dict1 = {'name':'lzc','age':'20','sex':'man'} print dict1 print type(dict1) >>> { ...

  8. 点聚-weboffice 6.0 (三)

    1.页面 var filename="<%=request.getParameter("filePath").toString()%>"; docu ...

  9. Oracle中日期和时间类函数

    首先,在oracle中如何表示日期 操作日期时,应使用to_date('date','dateType')函数得到date类型,其中date为任意格式的日期,dateType指定其格式,如to_dat ...

  10. 一起来学linux:进程

    简单来说,每当执行一个程序或者命令,启动事件的时候都会得到一个PID,也就是进程ID.比如用户登陆的时候就会得到一个PID.如下所示.两个用户zhf和root在登陆后分别获得PID 3212和3214 ...