【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 ...
随机推荐
- *389. Find the Difference (string + map(26)) read problems carefully
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- windows 右健添加cmd快捷通道
windows 右健添加cmd快捷 - Windows - geektown极客堂 - Powered by Discuz!. 把横线下面的文本copy保存到一个注册表文件中,比如cmd.reg,然后 ...
- IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)
四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft = 0, // 左对齐NSTextAlignme ...
- PHP获取系统时间不对的解决办法(转载)
原地址:https://blog.csdn.net/u012124764/article/details/51450958 使用PHP获取系统时间,发现时间不对,是因为PHP默认的时区是UTC,应该将 ...
- 【转】普及一下ARM和X86的区别,鉴于ATOM已经入驻手机,AMD也会…
这里简单来谈一下,ARM和X86之间为什么不太具有可比性的问题.要搞清楚这个问题首先要明白什么是架构,之前也有很多人提到了架构不同,但架构是什么意思?它是一个比较抽象的概念,不太容易用几句话就解释清楚 ...
- P2096 最佳旅游线路
最大字段和加贪心 算长了个见识吧 #include<iostream> #include<cstdio> #include<algorithm> using nam ...
- Java常见加密技术的密钥与加密串长度
Java常见的Java方式 1.Base64编码 2.十六进制(Hex)编码 3.MD消息摘要 4.DES加密 5.3DES加密 6.AES加密 6.RSA加密
- linux下Mycat的安装配置
1.下载Mycat Linux版:下载链接 2.通过SSH直连工具把安装包丢到linux:/usr/local/ 3.解压安装Mycat 4.配置环境 5.使配置文件生效
- JS实现数组去重的方法(6种)
方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Arra ...
- 使用JavaScript动态的绑定、解绑 a 标签的onclick事件,防止重复点击
页面上的 a 标签如下: <a class="more" style="cursor: pointer;" id="commentMore&qu ...