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. 简明python教程 --C++程序员的视角(三):模块

    模块和包 1 python程序由包(package).模块(module)和函数组成.包是由一系列模块组成的集合.模块是处理某一类问题的函数和类的集合.函数是一段可以重复多次调用的代码. 2 pyth ...

  2. Objective-C:MRC手动释放对象内存举例(引用计数器)

    手机内存下的类的设计练习: 设计Book类, 1.三个成员变量:    title(书名)author(作者).price(价格) 2.不使用@property,自己完成存取方法(set方法,get方 ...

  3. 混沌数学之二维logistic模型

    上一节讲了logistic混沌模型,这一节对其扩充一下讲二维 Logistic映射.它起着从一维到高维的衔接作用,对二维映射中混沌现象的研究有助于认识和预测更复杂的高维动力系统的性态.通过构造一次藕合 ...

  4. Informatica 常用组件Aggregator之三 使用排序输入

    可以使用排序输入选项改善聚合转换性能.使用排序输入时,PowerCenter 会假定所有数据已按组排序.PowerCenter 读取某组的行时,它将执行聚合计算.需要时,它会将组信息存储在存储器中.要 ...

  5. iOS开发-多线程之GCD(Grand Central Dispatch)

    Grand Central Dispatch(GCD)是一个强有力的方式取执行多线程任务,不管你在回调的时候是异步或者同步的,可以优化应用程序支持多核心处理器和其他的对称多处理系统的系统.开发使用的过 ...

  6. IOS网络访问详解

    第一.访问网络的方式 同步请求:数据的请求过程是由主线程发起的,网络加载需要一定的时间,因此会堵塞主线程 异步请求:数据的请求在多线程中完成 同步请求无法取消,异步请求的过程中可以取消,同步请求无法监 ...

  7. NTP Server

    Network Time Protocol互联网时间协议 NTP is intended to synchronize all participating computers to within a ...

  8. [Angular] Use ngx-build-plus to compile Angular Elements

    We can treat Angular Element as each standlone lib and compile each Angular element spreatly. Tool w ...

  9. Jquery attr("checked") 返回checked或undefined 获取选中失效

    $('#cb').attr('checked'); 返回的是checked或者是undefined,不是原来的true和false了,有关此问题的解决方法如下: <input type='che ...

  10. IOS开发基础Object-C(12)—单例模式

    单例模式的意思就是仅仅有一个实例. 单例模式确保某一个类仅仅有一个实例,并且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类仅仅能有一 ...