Multiply Strings

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.

 
 
各个位相乘,保存在数组中,最后再处理进位。
123*456
 
  4,5,6
     8,10,12
        12,15,18
________________
   4,13,28,27,18
 
即56088
 
 class Solution {
public:
string multiply(string num1, string num2) { if(num1==""||num2=="") return ""; while(num1[]=='') num1.erase();
while(num2[]=='') num2.erase(); int n1=num1.size();
int n2=num2.size(); int *numArr1=new int[n1];
int *numArr2=new int[n2]; int *result=new int[n1+n2];
memset(result,,sizeof(int)*(n1+n2));
for(int i=;i<n1;i++) numArr1[i]=num1[i]-'';
for(int i=;i<n2;i++) numArr2[i]=num2[i]-''; for(int i=;i<n1;i++)
{
for(int j=;j<n2;j++)
{
result[i+j+]+=numArr1[i]*numArr2[j];
}
} int carry=;
for(int i=n1+n2-;i>=;i--)
{
result[i]+=carry;
if(result[i]>=)
{
carry=result[i]/;
result[i]=result[i]%;
}
else
{
carry=;
}
} string resultStr;
int k=;
while(result[k]==) k++; for(int i=k;i<n1+n2;i++)
{
resultStr.push_back(result[i]+'');
} return resultStr;
}
};

【leetcode】Multiply Strings的更多相关文章

  1. 【leetcode】Multiply Strings(middle)

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

  2. 【Leetcode】【Medium】Multiply Strings

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

  3. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  4. 【leetcode】Isomorphic Strings(easy)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  5. 【LeetCode43】 Multiply Strings

    题目描述: 解题思路: java代码: public class LeetCode43 { public static void main(String[] args) { String num1=& ...

  6. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  7. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

随机推荐

  1. Lua弱引用table

    弱引用table 与python等脚本语言类似地,Lua也采用了自动内存管理(Garbage Collection),一个程序只需创建对象,而无需删除对象.通过使用垃圾收集机制,Lua会自动删除过期对 ...

  2. ActionScript学习笔记

    ActionScript学习笔记 ActionScript中预定义的数据类型:Boolean.int.Number.String.uint 其中,int.Number.uint是处理数字的.int用来 ...

  3. 判断QQ是否在线

    <html> <body> ggygygygy<br> <td><a href="http://wpa.qq.com/msgrd?V=1 ...

  4. mongodb 与 c++ 的配合使用

    最近在尝试使用 mongodb 作为服务端持久化方案,服务端程序是使用 c++ 写的,折腾了不少时间,记录一下吧. 1.下载 boost 1.56.0 http://www.boost.org/use ...

  5. Maven Eclipse (m2e) SCM connector for subclipse 1.10 (svn 1.8) 无法检测

    用新东西总是会有一些风险,尤其是相互的依赖和版本问题. 为了体验最新Eclipse Mars,Version: Mars Milestone 1 (4.5.0M1),Eclipse安装之后需要安装一些 ...

  6. AD域服务器|两台DC无法进行复制同步

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...

  7. 【bzoj3573】[HNOI2014]米特运输

    题目描述 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城 ...

  8. ajax如何返回多个值

    应用场景:  在前端有个ajax请求到后端后,需要返回多个变量的值,在这里使用的是Json格式作为值传递,使用eval函数来解析Json格式. 要传递的值data: var data = " ...

  9. c#中两种不同的存储过程调用与比较

    存储过程简介 简单的说,存储过程是由一些SQL语句和控制语句组成的被封装起来的过程,它驻留在数据库中,可以被客户应用程序调用,也可以从另一个过程或触发器调用.它的参数可以被传递和返回.与应用程序中的函 ...

  10. php 正则表达式的使用

    要点:php正则表达式要用双引号,且要用“/ /”斜线做开始结束. 1.preg_match . preg_match_all 两者的区别:第一次匹配成功后就会停止匹配,如果要实现全部结果的匹配,即搜 ...