43. Multiply Strings (String)
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.
思路:首先确定结果的位数,难点在于知道结果中的每一位,是乘数中的哪两位相乘贡献的:是num2中的i-len1+1到i中的数分别乘以num1中的i-j相加而得的,并且要注意不能超过num2的界限[0,len2-1]
class Solution {
public:
string multiply(string num1, string num2) {
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int carry = , sum =;
string result="";
int len1 = num1.length();
int len2 = num2.length();
int resLen = len1+len2-;
for(int i = ; i < resLen; i++){ //traverse the result digit
for(int j = max(,i-len1+) ; j <= min(i,len2-); j++){ //traverse the num2 digit
sum += (num2[j]-'')*(num1[i-j]-'');
}
sum += carry;
carry = sum/;
result += ((sum%) + '');
sum=;a
}
if(carry) result += (carry+'');
reverse(result.begin(),result.end());
if(result[]=='') return ""; //全0的情况
return result;
}
};
43. Multiply Strings (String)的更多相关文章
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【一天一道LeetCode】#43. Multiply Strings
一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...
随机推荐
- 杂项-Java:jar 包与 war 包介绍与区别
ylbtech-杂项-Java:jar 包与 war 包介绍与区别 1.返回顶部 1. 做Java开发,jar包和war包接触的挺多的,有必要对它们做一个深入的了解,特总结整理如下: 1.jar包的介 ...
- VMware vCenter Server安装
SQL Server 2008 R2安装包:http://blog.sina.com.cn/s/blog_4aedf6370101j9tz.html SQL Server安装指导:http://www ...
- 1082 Read Number in Chinese (25 分)
1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...
- 关于ESXI5.0版本+ Broadcom BCM5720的BUG
主要发生在网卡 Broadcom Corporation NetXtreme BCM5720 Gigabit Ethernet 和ESX 5.0之前的版本. 虚拟机的网络突然不通,必须删除网卡重新创建 ...
- EC20模块(mdm9607)复用pin脚当作普通gpio的设置方法
修改pin37~40,当作普通gpio的方法如下: 下面代码截图需要确认是否一样,如不一样请修改: 修改pin41~42 ,当作普通gpio方法如下: Pin24~27,当作普通gpio方法如下: P ...
- asp控制项目超时
If Session("username")="" or isnull(Session("username")) Then %> &l ...
- python入门-函数(一)
1定义函数并且调用 注释语句""" """ def greet_user(): """显示简单的问候语&qu ...
- Latex 箭头、下标、符号上下写文字、正方形和三角形
0. hat $abc\hat def$ $ab\widehat{cde}f$ 1. 箭头上的文字 $\underrightarrow{\text{你的文字}}$ ($A \xlefta ...
- php7---redis
wget https://github.com/phpredis/phpredis/archive/php7.zip cd /usr/local/src/ phpredis-php7 /usr/loc ...
- JDK9-模块化系统
Content 0. 实例 0.1 使用命令行编写和运行模块程序 0.1.1 设置目录 0.1.2 编写源代码 0.1.3 编译 0.1.4 打包模块代码 0.1.5 运行程序 0.2 使用eclip ...