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

有两个错误以前没在意,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. trunc()

    select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(trunc(sysdate), 'yyyy-mm-dd hh24:mi:ss') f ...

  2. Linux基础练习题(五)

    1.创建一个10G分区,并格式为ext4文件系统: (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl: [root@bj-1-142 ~]# ...

  3. Mybatis 配置文件

    1.核心配置文件 sqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...

  4. iOS日历显示农历信息

    第一次接触到日历的开发,表示需要学习的东西还有很多呢! 关于日历的开发,如果不进行相关设置的话,默认是没有农历的,需要我们进行设置. 核心Demo如下: monthArr = [NSArray arr ...

  5. php网易云信im即时通讯和聊天室

    话不多说 直接上代码 <?php/** * Created by PhpStorm. * User: lhl * Date: 2019/4/10 * Time: 17:38 */ namespa ...

  6. php wamp基础环境搭建

    一.apache 安装配置: 1.安装apache 1.1 下载地址:https://www.apachelounge.com/download/ 1.2 将下载的文件解压到你想安装的目录 D:\WA ...

  7. window7下 cmd命令行 Mysql导出表结构 + 表数据

    命令格式 mysqldump -uroot -p 密码 库名 > 自定义路径/库名.sql

  8. EDID的简介和解析

    去年对EDID做了一个解析,下面是学习EDID过程中整理的资料. 一.EDID简介 EDID: Extended Display Identification Data (外部显示设备标识数据)--- ...

  9. docker inspect获取详细参数的两种方法

    docker inspect xx 返回的是一个json格式的数据 以下为部分返回值 [ { "Id": "706813b0da107c4d43c61e3db9da908 ...

  10. C++快速开发样本工程的建立--编写常用组件

    在添加的main.h里面添加如下功能: 主要使用标准C++和boost,界面部分采用QT 1.运行目录环境 2.使用的字体 3.使用的样式 4.使用的主题