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. Date类型之继承方法

    ECMAScript中的Date类型是在早期Java中的java.util.Date类型基础上构建的.为此,Date类型使用自UTC(国际协调时间)1970年1月1日午夜零时开始经过的毫秒数来保存日期 ...

  2. [转]C# 系统应用之鼠标模拟技术及自动操作鼠标

    原文网址: C# 系统应用之鼠标模拟技术及自动操作鼠标        游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C# ...

  3. RESTful Java client with Apache HttpClient / URL /Jersey client

    JSON example with Jersey + Jackson Jersey client examples RESTful Java client with RESTEasy client f ...

  4. html如何设置打印样式?

    转自网络,忘记出处了. html/jsp/网页/打印相关/打印预览/js设置页眉页脚 <html> <head>    <title>打印相关</title& ...

  5. Spring MVC springMVC-servlet.xml

    DispatcherServlet的配置: DispatcherServlet是SpringMVC的前端控制器,所有的请求都经过前端控制器,也是项目中出现的唯一一个servlet,在 web.xml中 ...

  6. Solr学习总结(六)solr的函数查询Function Queries

    摘要: 函数查询允许你使用一个或多个数字字段的真实值生成一个相关性分数,函数查询在standard,DisMax,eDisMax下都能使用. 查询函数可以是常量,字段或者其他函数的组合.使用函数可以影 ...

  7. 同步锁源码分析(一)AbstractQueuedSynchronizer原理

    文章转载自 AbstractQueuedSynchronizer的介绍和原理分析 建议去看一下原文的评论,会有不少收获. 简介 AbstractQueuedSynchronizer 提供了一个基于FI ...

  8. Java网络编程详解

    内容: 1.网络通信协议 2.UDP与TCP 3.UDP通信 4.TCP通信 5.网络编程总结 1.网络通信协议 (1)基本概念 网络:由多台计算机以及外部设备连接起来的一个系统,我们称之为网络 通信 ...

  9. uva-10344

    题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...

  10. OpenCL 双调排序 GPU 版

    ▶ 参考书中的代码,写了 ● 代码,核函数文件包含三中算法 // kernel.cl __kernel void bitonicSort01(__global uint *data, const ui ...