题目:

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

  1. 【Add binary】cpp

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  2. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  3. 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~ ...

  4. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  5. 【Scramble String】cpp

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  6. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

随机推荐

  1. OpenGL纹理高级

    矩形纹理 对于二维纹理来说,除了GL_TEXTURE_2D之外,使用GL_TEXTURE_RECTANGLE就可以使用矩形纹理. 矩形纹理几大特点: 不能Mip,只能加载glTexImage2D的le ...

  2. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  3. Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用 首先我们看例程如下: (1) main.xml文件如下: <?xml version="1.0" encoding="utf-8& ...

  4. codeforces 600E Lomsat gelral

    题面:codeforces600E 学习一下$dsu \ on \ tree$.. 这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$. 主要操作是:我们先把整棵树链剖一下,然后每次 ...

  5. CPU体系结构

    http://blog.csdn.net/liuxc0116/article/details/17004313 1.算术逻辑单元ALU(Arithmetic Logic Unit)ALU是运算器的核心 ...

  6. Oracle 11g基础

    一.打开.关闭数据库 sqlplus "/as sysdba" connect system/manager as sysdba 关闭 shutdown immediate; 打开 ...

  7. redis hash类型

  8. Advanced Memory Allocation 内存分配进阶[转]

    May 01, 2003  By Gianluca Insolvibile  in Embedded Software Call some useful fuctions of the GNU C l ...

  9. 第1章-如何使用本书—零死角玩转STM32-F429系列

    第1章    如何使用本书 1.1    本书的参考资料 集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com ...

  10. HDU 2047 EOF牛肉串

    水到不想整理,线性DP #include <algorithm> #include <iostream> #include <cstring> #include & ...