leetcode个人题解——#43 Multiply Strings
思路:高精度乘法就可以了。
有两个错误以前没在意,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的更多相关文章
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 【一天一道LeetCode】#43. Multiply Strings
一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
随机推荐
- ios的framework合并
# 运行此脚本前 # 先编译一遍工程 确保正常运行 没有报错 # 作为Xcode Aggregate运行 # file-->new target-->cross-platform--> ...
- 05_Dockerfile实战(上)
在上一章我们介绍了docker镜像的概念,并且介绍了构建镜像的两种方式 使用docker commit命令提交创建的容器. 使用Dockerfile文件和docker build命令,这种更为推荐和常 ...
- NIO流—理解Buffer、Channel概念和NIO的读写操作
NIO流与IO流的区别 面向流与面向块 IO流是每次处理一个或多个字节,效率很慢(字符流处理的也是字节,只是对字节进行编码和解码处理). NIO流是以数据块为单位来处理,缓冲区就是用于读写的数据块.缓 ...
- Vue中,给当前元素添加类名移除兄弟元素类名的方法
在Vue中,给当前元素添加类名移除兄弟元素类名的方法 今天在项目中需要做一个效果,点击对应的li改变当前的color,其他的li取消颜色,在jQuery中这很容易,由于之前已经引入了jQuery,所以 ...
- IE浏览器中找不到开发者工具
ie浏览器不知道什么原因开发者工具不见了.打开以后在任务栏中显示打开了控制台,但是看不到. 解决方法 : F12 打开开发者工具后,按下 “ Ctrl + P ”
- js如何获取键盘高度
在移动端或混合app开发中,js如何获取键盘高度,直接贴上代码吧 input是一个html input 标签 var timer = { id:null, run:function (callback ...
- Myeclipse报错:The word is not correctly spelled
在eclipse下的Window--Preference输入spell,然后把第一个复选框“Enable spell checking"给去掉就可以了.
- Yar请求数据接口
//[['u'=>'site.index','d'=>['a'=>2],'k'=>'test']]; public function apiBatch($arr,$timeou ...
- php 二位数组排序
$member_ship_level 是一个二维数组 $res = array_column($member_ship_level,'integral'); array_multisort($res, ...
- 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of
4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...