【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 ...
随机推荐
- 模拟插队,出队,POJ(2259)
题目链接:http://poj.org/problem?id=2259 水题一个,就是要记录一下每个队列是否有人bool[i], #include <iostream> #include ...
- 【转】Android UI开发第三十一篇——Android的Holo Theme
好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- ELKB是什么?
ELKB是四个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana ,Beats. Elasticsearch是个开源分布式搜索引擎,提供搜集.分析.存储数据三大 ...
- python-列表常用功能介绍
一.列表(list) 1.定义列表 names = [] #定义空列表 names = ['a','b','c'] #定义非空列表 2.访问列表中元素 >>> names = ['a ...
- sql的where条件转换成mongdb筛选条件
解析字符串 filterModel1 and filterModel2 and (filterModel3 or filterModel4) 1.转换成mongo的筛选条件 /// <summa ...
- acc_set_device_num && acc_get_device_num例程
void acc_set_device_num( int, acc_device_t ); 描述在指定类型的所有设备中, acc_set_device_num 告诉运行时库环境用哪一个设备.如果 de ...
- JavaEE权限管理系统的搭建(二)--------聚合工程项目的创建和依赖关系
本项目是一个聚合工程,所以要先搭建一个聚合工程的框架 搭建完成的项目结构图如下: 首先创建父项目:pom类型 子模块:web层的搭建,war类型 把这个两个目录标记为对应的类型 其他子模块:和serv ...
- machine learning学习笔记
看到Max Welling教授主页上有不少学习notes,收藏一下吧,其最近出版了一本书呢还,还没看过. http://www.ics.uci.edu/~welling/classnotes/clas ...
- Java Web入门经典扫描版
全书共分4篇19章,其中,第一篇为“起步篇”,主要包括开启JavaWeb之门.不可不知的客户端应用技术.驾驭JavaWeb开发环境.JavaWeb开发必修课之JSP语法等内容:第二篇为“核心篇”,主要 ...