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. Android · PendingIntent学习

    Intent 是及时启动,intent 随所在的activity 消失而消失 PendingIntent用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转.   ...

  2. hint指定index的深入理解

    http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...

  3. UIView创建的两种方式

    //通过xib创建 NSBundle * bundle = [NSBundle mainBundle]; NSArray * arr = [bundle loadNibNamed:@"myV ...

  4. Spring利用propertyConfigurer类 读取.property数据库配置文件

    (1).基本的使用方法是: <bean id="propertyConfigurerForAnalysis" class="org.springframework. ...

  5. EntityFramework走马观花之CRUD(下)

    我在Entity Framework系列文章的CRUD上篇中介绍了EF的数据查询,中篇谈到了EF的数据更新,下篇则聊聊EF实现CRUD的内部原理. 跟踪实体对象状态 在CRUD上篇和中篇谈到,为了实现 ...

  6. python 基础 7.6 sys 模块

    一.sys 模块 sys 模块主要功能是获取参数     [root@www pythonscripts]# cat 2.py #!/usr/bin/python #coding=utf-8   im ...

  7. webstorm-----eslint的配置和使用

    https://blog.csdn.net/qq_29329037/article/details/80100450

  8. windows下的常用命令

    net start ... 启动某个服务 net stop ... 停止某个服务 net start     查看所有启动的服务 services.msc  打开服务的界面 ipconfig     ...

  9. SpringMVC @ResponseBody和@RequestBody使用

    @ResponseBody用法 作用: 该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConvert ...

  10. transition_matrix Markov chain