[LeetCode] 大数问题,相加和相乘,题 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.
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的更多相关文章
- LeetCode 43. 字符串相乘(Multiply Strings) 大数乘法
题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2" ...
- leetcode 第42题 Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- Leetcode——258.各位相加【水题】
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...
- LeetCode 43. 字符串相乘(Multiply Strings)
43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- 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) ...
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
随机推荐
- 352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- (转载)IE8+兼容经验小结
本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几点都关注过,那么基本上很大一部分IE8+兼容性问题都OK ...
- [C++] Copy Control (part 1)
Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...
- HTML5文档的各个组成部分分类
<!DOCTYPE html><!--声明文档结构类型--> <html lang="zh-cn"><!--声明文档文字区域--> ...
- 自测之Lesson4:gdb
题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...
- Python3.5在Windows7环境下Scrapy库的安装
Python3.5在Windows7环境下Scrapy库的安装 忙活了一下午,总算是把Scrapy库给装完了,记下来给需要帮助的人 首先安装的环境:Windows7 64位 Python的版本是:3. ...
- Notes of the scrum meeting before publishing2(12.18)
meeting time:18:30~20:30p.m.,December 18th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- onethink框架显示Access denied for user 'root'@'localhost' (using password: NO)
本地开发的时候使用的用户名是root,密码为空,它会生成两份.一份在Common/config.php里面,还有一份在Application\User\Conf/config.php 在linux环境 ...
- PART1 一些想法
其实我一直是一个后知后觉的人,这点也是我过了好久才发现的问题,之所以晚发现自己这个毛病,是因为后知后觉==,这有点像是个悖论或者是笑话,但的确是真实存在于我的身上.其实当初为啥来这个学校选计算机的专业 ...
- MFC消息处理
1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook().这个Hook将拦截 ...