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

有两个错误以前没在意,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. 安装maven提示ERROR: JAVA_HOME is set to an invalid directory.

    查询网上资料发现多种解决办法:有的是多写了分号,有的路径错误. 需要注意的是maven配置前需要配置好jdk的路径. 我的java_home 之前的配置为:C:\Program Files\Java\ ...

  2. 分布式架构学习-Consul集群配置

    简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...

  3. html中的meta元素及viewport属性值

    <meta name="viewport" content="width=device-width , initial-scale=1.0, maximum-sca ...

  4. center os 7 安装 elasticSeartch

    Centos7下安装配置elasticsearch 6.3.1   1)下载 Elasticsearch 6.3.1 地址:https://artifacts.elastic.co/downloads ...

  5. java web 常识

    model.addattribute()的作用: 1.往前台传数据可以传对象,List,通过el表达式${}获取,类似于request.setAttribute("sts",sts ...

  6. Oracle特殊查询 行列倒转 分页

    --查询工资最高的前三名 (分页的感觉)select * from(select * from emp order by sal desc) twhere rownum <=3--查询工资最高的 ...

  7. js toFixed()方法的坑

    javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...

  8. C++练习 | 递归创建二叉树并求叶子结点的数值和

    #include <iostream> using namespace std; struct Tree { int data; Tree *lchild; Tree *rchild; } ...

  9. 什么是ajax和json,说说他们的优缺点

    ajax异步传输的js和xml.实现页面无刷新状态更新页面和异步提交 所谓异步,简单解释就是:向服务器发送请求的时候,我们不必等待结果,而是同时做其他的事情,等到有了结果后它自己会根据设定进行后续操作 ...

  10. Redis集群入门

    官方文章: https://redis.io/topics/cluster-tutorial#redis-cluster-configuration-parameters 本文永久地址: https: ...