【LeetCode】43. Multiply Strings
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.
将乘积逐位逆序存放在int数组result中。
记num1当前为第i位,num2当前为第j位,则乘积存放在result[(n1-1-i)+(n2-1-j)]中。
注意进位,当num1[i]与num2的每一位都相乘之后,如果仍有进位,需要存放在result[(n1-1-i)+n2]中。
最后将result逆序,去掉前面的0.但是如果全为0就返回"0"。
class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.size();
int n2 = num2.size();
vector<int> result(n1+n2);
string resultStr;
for(int i = n1-; i >= ; i --)
{// for num1[i]
int carry = ;
int val1 = num1[i] - '';
for(int j = n2-; j >= ; j --)
{// for num2[j]
int val2 = num2[j] - '';
int res = val1 * val2;
int ind = (n1--i)+(n2--j);
result[ind] += carry;
result[ind] += res;
carry = result[ind] / ;
result[ind] %= ;
}
if(carry != )
{
int ind = (n1--i)+n2;
result[ind] += carry;
}
}
//reverse result
reverse(result.begin(), result.end());
int i;
for(i = ; i < n1+n2; i ++)
{
if(result[i] != )
break;
}
if(i == n1+n2)
//all 0
return "";
else
{
for(; i < n1+n2; i ++)
{
resultStr += (result[i] + '');
}
return resultStr;
}
}
};

【LeetCode】43. Multiply Strings的更多相关文章
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】43. Multiply Strings(大数相乘)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- 【一天一道LeetCode】#43. Multiply Strings
一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】205. Isomorphic Strings
题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...
随机推荐
- VMware vCenter中, 如何辩认虚机上Raw Device Mapping过了的一块物理磁盘?
比如说, 我们有一套VMware的环境, 其中有一台运行者ESXi的主机, 其上有十块SAS盘. 这十块盘中的五块盘被RDM到一台虚机上了. 假设你发现有添加多了一块盘, 你想移除掉, 但是5块盘其中 ...
- 反射 Reflect Modifier 修饰符工具类
在查看反射相关的Class.Field .Constructor 等类时,看到他们都有这样一个方法:getModifiers():返回此类或接口以整数编码的 Java 语言修饰符.如需要知道返回的值所 ...
- JavaScript事件冒泡机制和阻止事件冒泡及默认事件
一.阻止事件冒泡: 1.html中加return false 2.js中加return false 3.IE下:window.event.cancelBubble = true: FF下:event ...
- Android界面设计之对话框——定制Toast、AlertDialog
一.概述 在界面设计中需要根据用户操作显示提示信息.出错信息等,就要用到对话框.Android实现提示信息显示常用有两种方式 1.Toast 2.AlertDialog 二.Toast Android ...
- URAL 1698
题目大意:统计位数不大于n的自守数的个数. KB 64bit IO Format:%I64d & %I64u 数据规模:1<=n<=2000. 理论基础: 自守数:参见链接 ...
- 数据需求统计常用awk命令
原文:http://www.5iops.com/html/2013/script_0418/267.html 1.将时间转换为时间戳 select unix_timestamp('2009-10-26 ...
- Error Lookup工具
GetLastError()获取到的错误代码,可以通过VS2013自带的Error Lookup工具来查询错误的描述.
- mybatis的statement的解析与加载(springboot)
问题 mybatis的xml中的sql语句是启动时生成JDK代理类的时候就生成一次么 调用顺序链 解析xml配置 Reader reader = Resources.getResourceAsRead ...
- GG配置ggmgr进程
Oracle配置mgr进程 edit params mgr port 7809 syslog none dynamicportlist 7810-7820 自己主动会生成./dirprm/mgr.pr ...
- C#.NET常见问题(FAQ)-如何修改代码字体
工具-选项-字体和颜色 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: http://csrob ...