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 ...
随机推荐
- POJ 1182 食物链(并查集的使用)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81915 Accepted: 24462 Description ...
- appium 3-4-1034等待、日志、性能数据、xpath定位、web driver协议
1.等待 1.1精确等待 sleep 不推荐 @Test public void testWait1() throws InterruptedException{ day_time(); Thread ...
- [UE4]把枪抽象为一个类
- Solr Facet 搜索时,facet.missing = true 的真正含义
Solr的WiKI原文是如下解释: facet.missing Set to "true" this param indicates that in addition to the ...
- centos7的web环境安装配置
1.安装基本东西安装apache yum install httpd安装mariadb yum install mariadb mariadb-server安装php yum install p ...
- sqlserver查询---分配cpu等资源
数据库资源按需分配 https://www.cnblogs.com/i6first/p/4138365.html https://blog.csdn.net/kk185800961/article/d ...
- mysql:rand()产生随机整数,CONCAT拼接时间字符串
用存储过程插入测试数据,如果不想update_time都一样,可以进行随机字符串拼接:2月随机1天,小时随机 CONCAT('2017-02-',FLOOR(1 + (RAND() * 28)),' ...
- vue深入了解组件——插槽
一.插槽内容 Vue实现了一套内容分发的API,这套API基于当前的Web Components规范草案,将 <slot> 元素作为承载分发的内容的出口. 它允许你像这样合成组件: &l ...
- Linq相关
Linq(语言集成查询) 相关资料如下: 1. Linq语言集成查询 百度百科 2. 30分钟Linq教程 3. Linq查询表达式(C#编程指南) 4. Linq十个常用查询 5. Linq技术专题 ...
- ubuntu16.04安装tensorflow-gpu和cuda8.0加速训练
转载请注明出处:http://www.cnblogs.com/buxizhizhoum/p/8086230.html 环境: 系统:ubuntu 16.04 cpu:i5 gpu:gt920m mem ...