【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 ...
随机推荐
- ListView的多布局中的小问题
今天用到了ListView的多布局,我们需要额外重写两个方法 //返回多布局的个数 @Override public int getViewTypeCount() { return 3; } //用该 ...
- ajax & jsonp & img
ajax 是一种请求服务器的方式,核心是XMLHttpRequest对象: 优点是无需刷新页面, 缺点是不能跨域请求. /* * Ajax direacted by Zakas * * Ajax.ge ...
- 如何检测某IP端口是否打开
1.如果你直接到控制面板的管理工具里的服务项里去找telnet的话,那是徒劳无功 的,因为默认根本就没有这一服务.当然,你可以通过如下方式搞定.“控制面 板” 一〉“程序” 一〉“打开或关闭windo ...
- Data Developer Center > Learn > Entity Framework > Get Started > Loading Related Entities
Data Developer Center > Learn > Entity Framework > Get Started > Loading Related Entitie ...
- C# 生成XML 多级节点
直接上代码: 在应用程序中 class Program { //public static JsonServiceClient Service = new JsonServiceClient(Conf ...
- FPGA---ucf文件编写
摘要:本文主要通过一个实例具体介绍ISE中通过编辑UCF文件来对FPGA设计进行约束,主要涉及到的约束包括时钟约束.群组约束.逻辑管脚约束以及物理属性约束. Xilinx FPGA设计约束的分类 Xi ...
- linux校准时间
Linux下ntpdate时间同步 Ntp服务器安装配置 ntp(Network Time Protocol)协议 RedHat服务器可以下载rpm安装包,然后执行# rpm -ivh ntp-4.2 ...
- C++设计模式——享元模式
本文版权归果冻说所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.如果这篇文章对你有帮助,你可以请我喝杯咖啡. » 本文链接:http:// ...
- 局域网内Tomcat服务器没法访问
多半是防火墙的问题,在server2008上打开防火墙设置,关闭即可访问,不关闭的访问方式暂时好没研究出来
- c语言编写的日历
输入年份如2013,显示2013年的日历. 思路: 1.查找每个月1号是星期几(这里利用了1990年1月1号是星期一) 计算年份如2013年1月1号到1990年1月1号有Days天,Day%7得到星期 ...