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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【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 ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【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 ...

  5. 【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 ...

  6. 【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 ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. equals方法,hashcode()方法

    Object类的equals 方法 用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写.   (一)说到equals方法,不得不提一下==号. ==用于比较引用和比较原生数据类型时具有不同 ...

  2. vim代码补全-spf13,YouCompleteMe

    vim代码补全 现在的图形界面的IDE(Integrated Development Environment)一般具有语法高亮,语法检查,自动补全功能,大大提高了编程的效率. vim作为文本编辑器其强 ...

  3. servlet中的cookie

    cookie的机制是:从客户端(浏览器)发送请求到服务器,然后服务器把接受的信息回写到客户端,这个信息在客户端跟服务器之间进行交互. 下面是一个创建cookie的小案例 //如何创建cookie pa ...

  4. WebApp JS 打开 app

    产品需求:分享出去的链接比如到微信朋友圈,微博的H5页面,添加一个按钮 open App 用来打开并启动自己公司的APP (如果当前手机已经安装自己公司的APP) 废话少说直接上代码: <inp ...

  5. js的reduce方法,改变头等函数

    头等函数:把编程变成了类似搭积木的方式编码,可以使用很少的代码,实现强大的功能函数. eg: getTotal:数组的求和运算. var myArray = [1,2,3,4]; var add = ...

  6. android开发系列之代码整洁之道

    说起代码整洁之道,想必大家想到更多的是那本经典重构书籍.没错,记得当时自己读那本书的时候,一边结合项目实战,一边结合书中的讲解,确实学到了很多东西,对我自己的编码风格影响极深.随着时间的流逝,书中很多 ...

  7. iOS学习之C语言指针

     访问数据的两种方式: 1.直接访问: 2.间接访问:先找到内存地址,根据地址访问存储单元.(内存地址 被称为指针) 定义指针变量: 类型修饰符 *指针变量名 = NULL; NULL表示空指针,相当 ...

  8. Android Jni变量对照表

    字符 Java类型 C类型 V      void            void Z       jboolean     boolean I        jint              in ...

  9. 受限玻尔兹曼机RBM—简易详解

  10. VMWare EXSi 添加新磁盘时 报错 HostStorageSystem.ComputeDiskPartitionInfo 的处理

    给 VMWare EXSi 添加新磁盘时报错 : Call "HostStorageSystem.ComputeDiskPartitionInfo" for object &quo ...