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. Windwos下常用DOS命令

    1.添加用户命令: net user 用户名 密码 /add 2.将用户加入组的命令: net localgroup administrators 用户名 /add 3.在dos命令行模式下启用用户: ...

  2. Log4Net使用方法

    项目里都会用到日志记录..特别是在本地跑的欢畅..一上服务器就嗝屁的时候..日志会帮你大忙.. Log4net算是一种应用的最广泛的日志记录方式了..下面来简略的说下他的用法.. 先下载log4net ...

  3. Block 及注意事项

    block 概念 block 是 C 语言的 是一种数据类型,可以当作参数传递 是一组预先准备好的代码,在需要的时候执行 block 的注意事项 (1)block 在实现时就会对它引用到的它所在方法中 ...

  4. Devexpress-1 DataGrid控件

    参考资料: 使用XtraGrid自定义列计算 DEV GridControl小结 实现对两列求和后作为新的列

  5. JQ nextALL() 实现遍历

    在我们的 手机端 常常需要 应用到 hide 和 show 的方法 来节省页面的版块 那么我们需要更多的是 需要 show 一个 <li>  hide 剩下的 <li> 那么 ...

  6. 微信或移动端网页的meta

    针对微信: <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> ...

  7. [百度地图] ZMap 与 MultiZMap 封装类说明;

    ZMap.js 与 MultiZMap 说明 1. ZMap 与 MultiZMap 都是封装一些地图常用的使用方法,类方法功能大多使用 prototype 原型 实现: ZMap 在一个页面只能使用 ...

  8. [译]git commit

    git commit git commit命令提交stage区的快照到项目历史中去(HEAD). 被提交的快照被认为是一个项目的安全版本. Git不会修改他们, 除非你显示的要求了. 和git add ...

  9. Outlook不能打开附件(提示:无法创建文件xx,请右键单击要在其中创建文件的文件夹..)

    问题分析: 出现这种问题的几率很小,除非你是每天都需要使用Outlook的办公人员.出现这种问题我想有如下两种可能.1.注册表中指定的附档临时保存的目录没有写入的相关权限.2.同名附档已存在且权限出现 ...

  10. 【bzoj2435】[NOI2011]道路修建

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...