思路:高精度乘法就可以了。

有两个错误以前没在意,1、成员属性定义时候不能进行初始化,

vector<int> a();

这样隐性调用了函数进行初始化的形式特别要注意,也是错误的;

2、容器类只有分配了空间时才能用=赋值,否则要用push_back之类函数插入元素。

class Solution {
public: void add(vector<int>& res, vector<int>& ans, int shift)
{
int sum = ;
int i;
for(int j = ;j < shift;j++)
res.insert(res.begin(), );
for(i = ; i < res.size(); i++)
{
int a = ans[i] + res[i] + sum;
ans[i] = a % ;
sum = a / ;
}
ans[i] += sum;
if(ans[i] >= ){
ans[i + ] = ans[i] / ;
ans[i] %= ;
}
} string vectorToString(vector<int>& ans){
int i = ans.size() - ;
string x;
while(ans[i] == ) i--; for(int j = i; j >= ; j--){
x += (ans[j] + '');
} return x;
} string multiply(string num1, string num2) {
const int size = (num1.size()+) *(num2.size()+);
vector<int> ans(size+,);
vector<int> res;
if(num1 == "" || num2 == "") return "";
vector<int> nums1,nums2;
for(int i = ; i < num1.size(); i++){
nums1.push_back(num1[i] - '');
} for(int i = ; i < num2.size(); i++){
nums2.push_back(num2[i] - '');
} for (int i = nums1.size() - ; i >= ; i--)
{
int sum = ; for(int j = nums2.size() - ;j >= ; j--)
{
int a = nums1[i] * nums2[j] + sum;
sum = a / ;
res.push_back(a % );
}
if(sum > ) res.push_back(sum);
add(res, ans,nums1.size() - i - );
vector <int>().swap(res);
}
return vectorToString(ans);
}
};

leetcode个人题解——#43 Multiply Strings的更多相关文章

  1. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  4. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  5. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  6. 【一天一道LeetCode】#43. Multiply Strings

    一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...

  7. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  9. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

随机推荐

  1. Java中的集合框架-Map

    前两篇<Java中的集合框架-Commection(一)>和<Java中的集合框架-Commection(二)>把集合框架中的Collection开发常用知识点作了一下记录,从 ...

  2. 单片机采集的MPU6050原始数据对应关系

    转自:https://blog.csdn.net/u013636775/article/details/69668860 单片机采集的MPU6050原始数据对应关系 1.陀螺仪 如下图, 陀螺仪的范围 ...

  3. 将变量做为一个对象的key,push新增进一个数组

    var orgnIdListValue=["0","2"]; function arrayField(a,b){ let arrayMes=[]; for(va ...

  4. easyui图标

    只要在icons属性上,加上图标对应的名字,easyUI就会显示对应的图标,这些图标都是easyui内置的.

  5. 记利用frp配合nginx实现内网透传

    frp下载 背景 : 内网有一台服务器A 在NAT背后 无法被其他客户端访问 借助公网服务器B来配置内网透传 即可通过B来访问A 服务端安装frps 启动: ./frps -c frps.ini 配置 ...

  6. JavaScript入门学习(0)相关 软件工具

    JavaScript本地脚本编辑工具(1st JavaScript Editor Pro ) 必要设置     https://pan.baidu.com/s/1XoaNA9o0qt2eJfLgoZ5 ...

  7. goland实现函数式链式编程

    先来看一段代码 package main import ( "fmt" elastic "gopkg.in/olivere/elastic.v2" ) type ...

  8. Fiddler抓取手机APP数据包

    第一步:下载神器Fiddler,下载链接: http://w.x.baidu.com/alading/anquan_soft_down_ub/10963 下载完成之后,傻瓜式的安装一下了! 第二步:设 ...

  9. 【非原创】Game23

    #include<stdio.h>int main(){ int n,m,x=0,flag=0; scanf("%d%d",&n,&m); x=m/n; ...

  10. Angular 7 版本

    Angular 7 版本 这是跨整个平台的主要版本,更新包括核心框架,Angular Material和CLI. 如何更新到v7 可以访问update.angular.io以获取有关更新应用程序的详细 ...