【Multiply Strings】cpp
题目:
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) {
const int n1 = num1.size(), n2 = num2.size();
if ( num1=="" || num2=="") return "";
vector<char> ret;
for ( int i=n1-; i>=; --i )
{
vector<char> local(,'');
int v = , carry = , curr = ;
for ( int j=n2-; j>=; --j )
{
v= (num1[i]-'')*(num2[j]-'');
carry = v/;
curr = v%;
carry += ((local[]-'')+curr)/;
curr = ((local[]-'')+curr)%;
local[] = curr+'';
local.insert(local.begin(), carry+'');
}
if (local[]=='') local.erase(local.begin());
// cout << string(local.begin(),local.end()) << endl;
// add zeros
for ( int z=n1-; z>i; --z) local.push_back('');
// cout << string(local.begin(),local.end()) << endl;
carry = , curr = ;
for ( int r=ret.size()-, l=local.size()-; r>= || l>=; --r,--l )
{
if ( r>= && l>= )
{
curr = (carry+(ret[r]-'')+(local[l]-''))%;
carry = (carry+(ret[r]-'')+(local[l]-''))/;
local[l] = curr+'';
}
else
{
curr = (carry+(local[l]-''))%;
carry = (carry+(local[l]-''))/;
local[l] = curr+'';
}
}
if (carry!=) { local.insert(local.begin(), carry+''); }
ret = local;
}
return string(ret.begin(),ret.end());
}
};
tips:
就是一些字符串操作的细节,考虑进位,0这类的细节。
======================================
第二次过这道题,憋了好久。主要是有个思维误区,认为tmp比ret最多多一位;但是,比如52*4这样的例子,2*4=8 50*4=200就不止一位了。
解决了这个误区,代码AC了。
class Solution {
public:
string multiply(string num1, string num2) {
if ( num1=="" || num2=="" ) return "";
vector<char> ret(num2.size(),'');
for ( int i=num1.size()-; i>=; --i )
{
// mupltiply ret
vector<int> tmp;
int val = , carry = ;
for ( int j=num2.size()-; j>=; --j )
{
val = (num2[j]-'')*(num1[i]-'')+carry;
tmp.insert(tmp.begin(), val%);
carry = val/;
}
if ( carry> ) tmp.insert(tmp.begin(), carry);
for ( int k=; k<num1.size()--i; ++k ) tmp.push_back();
// merge tmp & ret
while ( tmp.size()>ret.size() ) ret.insert(ret.begin(),'');
val = ;
carry = ;
for ( int m=tmp.size()-; m>=; --m )
{
val = (ret[m]-'') + tmp[m] + carry;
ret[m] = val%+'';
carry = val/;
}
if ( carry> ) ret.insert(ret.begin(), carry+'');
}
return string(ret.begin(), ret.end());
}
};
【Multiply Strings】cpp的更多相关文章
- 【Add binary】cpp
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Divided Two】cpp
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
- 【Scramble String】cpp
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
随机推荐
- selenium添加cookie切换到不同环境
背景:网站中需要切环境到预发布,在用谷歌浏览器可以使用工具,但是在selenium启动时,是不会带任何插件,向开发了解下,切换环境本质是添加cookie值,那么这个就简单了 1.使用selenium中 ...
- 最长上升子序列&&最长不下降子序列
百练2757: 题目描述: 对于给定的序列,求出最长上升子序列的长度. 题目链接:http://bailian.openjudge.cn/practice/2757 解题思路 一.动态规划 1. 找子 ...
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) C】最小边覆盖(简单题)
点此看题面 大致题意: 给你一个边集的子集,问你这可不可能是这张图的最小边覆盖. 大致思路 考虑到,如果一条边连接的两个点度数都大于等于\(2\),则这条边完全可以删去. 因此,我们只要判断是否存在这 ...
- paper-list
1.yolo-v1,yolo-v2,yolo-v3 2.ssd,focal loss,dssd 3.fast-rcnn,faster-rcnn,r-fcn,Light-Head R-CNN,R-FCN ...
- 利用python的numpy创建矩阵并对其赋值
创建一个3X3的矩阵并对其赋值: x = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print x print x.shape 运行结果: [[ ] [ ] [ ] ...
- CentOS上安装mongodb
安装mongodb pymongo修改yum源vi /etc/yum.repos.d/10gen.repo[10gen]name=10gen Repositorybaseurl=http://down ...
- MyBatis简单了解
MyBatis 是什么? MyBatis 是一个简化和实现了 Java 数据持久化层(persistence layer)的开源框架,它抽象了大量的 JDBC 冗余代 码,并提供了一个简单易用的 AP ...
- jquery操作DOM 元素(2)
.after() 在匹配的元素集合中的每个元素后面插入参数指定的内容,作为其兄弟节点. .after(content[,content]) content HTML字符串 DOM 元素 元素数组 对象 ...
- vs code配置c/c++调试环境+mingw+windows
目录 1.安装codeblocks 2.配置mingw环境变量 3.配置.vscode文件夹的json文件 ref 1.安装codeblocks 我已经安装过vscode和c++扩展插件,现在需要g+ ...