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.

class Solution {
public:
string multiply(string num1, string num2) {
}
};

我的解法是每次提取num2的一位,然后和num1相乘,所得结果并入string res中,这样虽然每次都要新定义一个string,效率略低,但是思路比较清晰:把字符串相乘划分成了“字符串和数字相乘” 和 “字符串相加” 两个子问题,分别解决就可以。

处理时先将num1,和num2逆序,使得最低位在[0] 位置,这样处理起来比较方便,不易写错。

class Solution {
public:
string multiply(string num1, string num2) {
if(num1.length() == || num2.length() == )
return "";
if(num1.length() == && num1[] == '') return "";
if(num2.length() == && num2[] == '') return "";
string res(num1.length() + num2.length(), '');
int i = , j = ;
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
for(i = ; i < num2.length(); ++i){
int offset = i;
string tmp(offset + num1.length() + , '');
int add = ;
for(j = ; j < num1.length(); ++j){
int tmpMul = (num1[j] - '') * (num2[i] - '') + add;
add = tmpMul / ;
tmp[j + offset] = (tmpMul % ) + '';
}
tmp[j + offset] = add + '';
plus(res, tmp);
}
int countStartZero = ; //高位'0'的个数
for(i = res.length() - ; i >= && res[i] == ''; --i, ++countStartZero);
if(countStartZero == res.length()) return "";
res = res.substr(, res.length() - countStartZero);
reverse(res.begin(), res.end());
return res;
}
private:
void plus(string &s1, string &s2){
int add = ;
for(int i = ; i < s1.length() && (add > || i < s2.length()); ++i){
int tmp = (s1[i] - '') + add;
if(i < s2.length()) tmp += (s2[i] - '');
add = tmp / ;
s1[i] = (tmp % ) + '';
}
}
};

[LeetCode] 大数问题,相加和相乘,题 Multiply Strings的更多相关文章

  1. LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法

    题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2" ...

  2. leetcode 第42题 Multiply Strings

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

  3. Leetcode——258.各位相加【水题】

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  4. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

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

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

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

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

  7. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. leetcode面试准备:Multiply Strings

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

随机推荐

  1. 《机器学习实战》笔记——决策树(ID3)

    现在要介绍的是ID3决策树算法,只适用于标称型数据,不适用于数值型数据. 决策树学习算法最大的优点是,他可以自学习,在学习过程中,不需要使用者了解过多的背景知识.领域知识,只需要对训练实例进行较好的标 ...

  2. “Hello World!团队”Alpha发布—视频链接+文案+美工

    视频链接:http://v.youku.com/v_show/id_XMzEyNjc2MTAyOA==.html?sharefrom=iphone&sharekey=5378037f8b710 ...

  3. Java学习个人备忘录之多态

    对象的多态性 class 动物 {} class 猫 extends 动物 {} class 狗 extends 动物 {} 猫 x = new 猫();//意思是建立本类的对象 new 猫(),并通 ...

  4. 20145214 《Java程序设计》第6周学习总结

    20145214 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 输入串流代表对象为java. ...

  5. Ubuntu录制gif动态图

    大神写博客的时候通常一个Demo会附带一个动态图展示效果.在windows和mac上应该很容易找到录制工具,下面记录一下我在ubuntu下录制gif的过程. 下载byzanz录制工具 在ubuntu软 ...

  6. iOS-addSubView时给UIView添加效果

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; ...

  7. TCP系列39—拥塞控制—2、拥塞相关算法及基础知识

    一.拥塞控制的相关算法 早期的TCP协议只有基于窗口的流控(flow control)机制而没有拥塞控制机制,因而易导致网络拥塞.1988年Jacobson针对TCP在网络拥塞控制方面的不足,提出了& ...

  8. 菜鸟的飞翔日记-os篇

    一轮王道os复习感想 1概述 虽然去年有上操作系统这门必修课,考的成绩也算理想,本来还有点沾沾自喜,嗯,觉得自己学的还不错,知道有一天我拿起了王道,(没给王道打广告)看王道的原因完全在于为考研做准备, ...

  9. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  10. [计算机网络] 互联网协议栈(TCP/IP参考模型)各层的主要功能及相应协议

    应用层:提供用户与网络间的接口.----HTTP.FTP.SMTP 运输层:进程到进程间的数据传输.---TCP.UDP 网络层:主机到主机之间的数据传输.---IP.选路协议 数据链路层:相邻结点之 ...