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

  1. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【leetcode】43. Multiply Strings(大数相乘)

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

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

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

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

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

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

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

  6. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  7. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  8. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  9. 【LeetCode】205. Isomorphic Strings

    题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...

随机推荐

  1. 用java查询HBase中某表的一批数据

    java代码如下: package db.query; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...

  2. 第十四章 Executors源码解析

    前边两章介绍了基础线程池ThreadPoolExecutor的使用方式.工作机理.参数详细介绍以及核心源码解析. 具体的介绍请参照: 第十二章 ThreadPoolExecutor使用与工作机理 第十 ...

  3. java 解析 XML实例

    package com.hseact.fecp.servlet; import java.io.IOException; import javax.xml.parsers.DocumentBuilde ...

  4. 在asp.net中使用jQuery实现类似QQ网站的图片切割效果

    今天要给大家介绍一个asp.net结合jQuery来切割图片的小程序,原理很简单,大致流程是: 加载原图 --> 用矩形框在原图上选取区域并将选取的顶点坐标和矩形尺寸发送至服务器 --> ...

  5. C#中Serializable序列化

    序列化就是是将对象转换为容易传输的格式的过程,一般情况下转化打流文件,放入内存或者IO文件 中.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象,或 ...

  6. Callable、Future&阻塞队列&阻塞栈

    Callable.Future   简单应用 在Java5之前,线程是没有返回值的,常常为了“有”返回值,破费周折,而且代码很不好写.或者干脆绕过这道坎,走别的路了.现在Java终于有可返回值的任务( ...

  7. 程序员们必备的10款免费jquery插件

    本周带来10款免费的jquery插件.如果你也有好的作品,欢迎分享到社区中来,在得到帮助的同时,也能与更多人分享来自你的作品. jQuery导航菜单置顶插件 - stickyUp . 在线演示 sti ...

  8. angular5使用httpclient时解决跨域问题

    跨域问题的环境: 在本地开发时,使用命令行ng s开启服务,访问地址为http://localhost:4200 假设调用数据接口的服务地址为http://localhost:8088/api/dat ...

  9. UITableViewAutomaticDimension

    tableView.rowHeight = UITableViewAUtomaticDimension UITableViewAutomaticDimension is the default val ...

  10. Java面试处理高并发

    经过查资料,方案如下所示.   1 从最基础的地方做起,优化我们写的代码,减少必要的资源浪费.           a.避免频繁的使用new对象,对于整个应用只需要存在一个实例的类,我们可以使用单例模 ...