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)的更多相关文章

  1. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  4. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  5. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  6. LeetCode(43. Multiply Strings)

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

  7. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  8. 【LeetCode题意分析&解答】43. Multiply Strings

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

  9. 【一天一道LeetCode】#43. Multiply Strings

    一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...

随机推荐

  1. JSON: JSON 用法

    ylbtech-JSON: JSON 用法 1. JSON Object creation in JavaScript返回顶部 1. <!DOCTYPE html> <html> ...

  2. gradle使用心得

    gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...

  3. MySQL Binlog三种格式介绍及分析

    Mysql binlog日志有三种格式,分别为Statement,MiXED,以及ROW! 1.Statement:每一条会修改数据的sql都会记录在binlog中. 优点:不需要记录每一行的变化,减 ...

  4. php multicast多播实现详解

    什么是多播? 网络中存在3中传播形式,单播,广播,多播. 1. 单播 : 就是1->1 2. 广播 : 1->多(广播域内) 3. 多播 : 1->组(一组ip) 1 2 3 4 5 ...

  5. Hessian与Webservice的区别

    Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单.快捷. 采用的是二进制RPC协议,因为 ...

  6. delphi WebBrowser的使用方法详解(四)-webbrowser轻松实现自动填表

    webbrowser轻松实现自动填表 步骤如下:  第一步:获取网页 调用Webbrowser 的Navigate系列函数.等待网页装载完成,得到document对象. 在调用 webBrowser. ...

  7. 文件系统性能测试--iozone

    iozone 一个文件系统性能评测工具,可以测试Read, write, re-read,re-write, read backwards, read strided, fread, fwrite, ...

  8. Linux下Nagios的安装与配置 及遇到的坑

    原文http://www.jianshu.com/p/7bc822fa8278 不愿意看前5.6c部分可以直接跳到最后看命令. 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能 ...

  9. MONGODB用户、角色和权限管理

      最近遇到很多问MONGODB用户认证授权方面的问题,现在特记录下来,与大家共享. 一.概念理解 1.用户概念 Mongodb的用户是由 用户名+所属库名组成 例如: 登录mongo  testdb ...

  10. 2017-2018-2 20165233 实验三 敏捷开发与XP实践

    20165233 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 一.编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及 ...