题目:

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. vuejs中v-if的深层用法v-else,v-else-if,key

    <div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...

  2. Nginx+Tomcat+memcached高可用会话保持

    Nginx+Tomcat+memcached高可用会话保持 文章来源dyc2005   一.概述 之前文章已经描述了企业高可用负载相关的架构及实现,其中常用的nginx或haproxy,LVS结合ke ...

  3. 【转】android gravity属性 和 weight属性

    有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...

  4. 【转】Android BroadcastReceiver介绍

    本文主要介绍BroadcastReceiver的概念.使用.生命周期.安全性.分类.特殊的BroadcastReceiver(本地.粘性.有序.粘性有序广播).示例代码见BroadcastReceiv ...

  5. nodejs 的一些PHP函数库

    http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...

  6. Compass Card Sales(模拟)

    Compass Card Sales 时间限制: 3 Sec  内存限制: 128 MB提交: 35  解决: 13[提交] [状态] [讨论版] [命题人:admin] 题目描述 Katla has ...

  7. python 合并字符串

    [root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ first_name = "ch ...

  8. java对象传递小解析

    先上代码: import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder ...

  9. 一篇SSM框架整合友好的文章(一)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/53730333 本文出自方志朋的博客 最近实在太忙,之前写的<rxjava系列文章&g ...

  10. 2018/7/16 YMOI模拟 NOIP2013D2T3华容道

    题目描述 Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. ...