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

有两个错误以前没在意,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. Web | JavaScript的引用数据类型强制转换类型

    我在这里主要的想提下的是JavaScript中的引用类型进行强制转换类型.因为对于基本数据类型的变换大多都是雷同的,很容易熟知,但是引用数据类型有一点小插曲. JavaScript的引用类型主要为对象 ...

  2. Springboot 启动文件报错,原因是@ComponentScan写成了@ComponentScans

    Springboot 启动文件报错,原因是@ComponentScan写成了@ComponentScans

  3. form组件-字段

    Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML 1.Django内置字段如下: Field required=True, 是否 ...

  4. Java并发编程(十)死锁

    哲学家进餐问题 并发执行带来的最棘手的问题莫过于死锁了,死锁问题中最经典的案例就是哲学家进餐问题:5个哲学家坐在一个桌子上,桌子上有5根筷子,每个哲学家的左手边和右手边各有一根筷子.示意图如下: 哲学 ...

  5. React简单实现双向数据绑定

    import React, { Component } from 'react' import ReactDOM from 'react-dom' class App extends Componen ...

  6. Windows Server2012,启动黑屏,只会弹出一个cmd命令窗口的解决办法

    Windows Server2012 服务器.在添加删除一个角色功能的时候,有可能会误删除Net Framework 4.5这个电脑基本功能组件. 就会影响到GUI界面的显示,所以服务器打开就只会黑屏 ...

  7. 一个数据仓库时代开始--Hive

    一.什么是 Apache Hive? Apache Hive 是一个基于 Hadoop Haused 构建的开源数据仓库系统,我们使用它来查询和分析存储在 Hadoop 文件中的大型数据集.此外,通过 ...

  8. 《Redis设计与实现》- 数据库

    1. 服务器中数据库结构 Redis 服务器将所有数据库都保存在服务器状态 redisServer 结构的 db 数组中,由 redisDb 结构代表一个数据库 struct redisServer ...

  9. homebrew 使用心得

    ''' 安装anaconda 安装命令: brew search anaconda brew cask install anaconda 添加环境变量: vi ~/.bash_profile expo ...

  10. Leecode刷题之旅-C语言/python-231 2的幂

    /* * @lc app=leetcode.cn id=231 lang=c * * [231] 2的幂 * * https://leetcode-cn.com/problems/power-of-t ...