【leetcode】Multiply Strings(middle)
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.
思路:直观思路,就是模拟乘法过程。注意进位。我写的比较繁琐。
string multiply(string num1, string num2) {
vector<int> v1, v2, vmulti;
vector<vector<int>> vvtmp;
int l1 = num1.size();
int l2 = num2.size();
if(l1 == && num1[] == '') return "";
if(l2 == && num2[] == '') return "";
int maxl = ;
//用vector存储两个数字 v[0]是最高位
for(int i = ; i < l1; i++)
v1.push_back(num1[i] - '');
for(int i = ; i < l2; i++)
v2.push_back(num2[i] - '');
//乘步骤
for(int i = l1 - ; i >= ; i--)
{
vector<int> vtmp; //v[0]是最低位
int t = i;
while(t < l1 - ) //后面补错位的0
{
vtmp.push_back();
t++;
}
int c = ; //记录进位
for(int j = l2 - ; j >= ; j--)
{
int cur = v1[i] * v2[j] + c;
vtmp.push_back(cur % );
c = cur / ;
}
if(c != )
vtmp.push_back(c);
vvtmp.push_back(vtmp);
maxl = (vtmp.size() > maxl) ? vtmp.size() : maxl;
}
//加步骤
int c = ; //记录进位
for(int i = ; i < maxl; i++)
{
int cur = c;
for(int j = ; j < vvtmp.size(); j++)
{
if(i >= vvtmp[j].size())
continue;
cur += vvtmp[j][i];
}
vmulti.push_back(cur % );
c = cur / ;
}
if(c != )
vmulti.push_back(c);
//转换为string
string ans;
for(int i = vmulti.size() - ; i >= ; i--)
{
ans += (vmulti[i] + '');
}
return ans;
}
大神总是能把多个步骤一次到位:
string multiply(string num1, string num2) {
string sum(num1.size() + num2.size(), '');
for (int i = num1.size() - ; <= i; --i) {
int carry = ;
for (int j = num2.size() - ; <= j; --j) {
int tmp = (sum[i + j + ] - '') + (num1[i] - '') * (num2[j] - '') + carry; //把当前乘出来的数字和之前的数字以及进位相加
sum[i + j + ] = tmp % + '';
carry = tmp / ;
}
sum[i] += carry; //这个是最高位多出来的进位 其他的都是i+j+1 这里没有+1
}
size_t startpos = sum.find_first_not_of("");
if (string::npos != startpos) {
return sum.substr(startpos);
}
return "";
}
【leetcode】Multiply Strings(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- 九度oj 1523 从上往下打印二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1523 建树,再层次遍历bfs.为了找根方便些,加了father指针... 如下: #include<algo ...
- 网络爬虫by pluskid
网络爬虫(Web Crawler, Spider)就是一个在网络上乱爬的机器人.当然它通常并不是一个实体的机器人,因为网络本身也是虚拟的东西,所以这个“机器人”其实也就是一段程序,并且它也不是乱爬,而 ...
- 设计移动App的十大技巧
编写一款Android或iOS应用也许很容易,但是若想设计的成功却不是一件简单的事,用户界面对于一款移动应用的成功是至关重要的.也许你会说,为何界面那么糙的Flappy Bird可以大红大紫,可那毕竟 ...
- wordpress nginx 开启链接为静态
使用固定连接里的自定义 /%postname%/ 日志标题的缩略版本(日志/页面编辑界面上的日志别名).因此“This Is A Great Post!”在URI中会变成this-is-a-great ...
- C#泛型集合之Dictionary<k, v>使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- python djange输入中文错误的解决办法
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) ...
- 数组和字典 swift
var array = ["A","B"] var array2:[String] = ["A","B"] var ar ...
- Travis-CI的进一步使用
今天主要对.travis.yml文件和makefile进行进一步的了解: 1.在.travis.yml文件中添加了给linux系统中安装了cppunit库的语句,使能够持续集成写过的单元测试的代码.主 ...
- html textarea换行和dom换行
从事开发已经两年多了,但是还是不会发现问题找原因,可能是自己一直在学校养成的习惯吧,不过最近在葛经理的带领下开始学会找原因了,而且发现自己变得更成熟了. 现在讲讲textarea和dom的换行吧,我们 ...
- SAXReader
DOM4j读取XML文件(SAXReader) 一. 总结: Document document=new SAXReader.reader(“xml文路径/文件名xxx.xml”);//得到Docum ...