#leetcode刷题之路43-字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
示例 1:
输入: num1 = "2", num2 = "3"
输出: "6"
示例 2:
输入: num1 = "123", num2 = "456"
输出: "56088"
说明:
num1 和 num2 的长度小于110。
num1 和 num2 只包含数字 0-9。
num1 和 num2 均不以零开头,除非是数字 0 本身。
不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。
#include <iostream>
#include <string.h>
using namespace std; string multiply(string num1, string num2) {
int l1=num1.length();
int l2=num2.length();
int i;
string ans;
if(l1 == || l2 == ) return "";
if(num1 == "" || num2 == "") return "";
int *t = new int[l1+l2];
//memset(t,0, (l1+l2)* sizeof(int));
fill(t,t+l1+l2,);
for(i = l1-; i >=; i--) {//从个位向前
for(int j =l2-; j >=; j--) {//从个位向前
int multi = (num1[i]-'') * (num2[j]-'');//存对应的数字相乘的结果
multi += t[i+j+];//先把这一次的值加上之前的最后一位,看会不会进位
t[i+j] += multi/;
t[i+j+] = multi % ;
}
}
i = ;
while(i < l1+l2 && t[i] == )
i++;//找到非零数的位置
for(; i < l1+l2; i++)
ans+=to_string(t[i]);
return ans;
} int main() {
string s1="";
string s2="";
string ans=multiply(s1,s2);
std::cout << ans << std::endl;
return ;
}
#leetcode刷题之路43-字符串相乘的更多相关文章
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- #leetcode刷题之路6- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L C I ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
随机推荐
- phpAdmin修改密码后拒绝访问
[phpMyadmin没配置正确] 解决方法: 1.打开 phpMyadmin 目录找到config.inc.php文件2.查找到$cfg['Servers'][$i]['password']=''3 ...
- redis 管道技术 pipeline 简介
redis数据库的主要瓶颈是网络速度,其次是内存与cpu.在应用允许的情况下,优先使用pipeline批量操作.pipeline批量发出请求/一次性获取响应:不是发出多个请求,每个请求都阻塞等待响应, ...
- maven 安装与配置最佳实践
配置Maven环境变量 1.新建 maven home 环境变量 变量名:M2_HOME 变量值:D:\ProgramFiles\apache-maven-3.5.4 2 ...
- TYPE_SCROLL_INSENSITIVE is not compatible with CONCUR_UPDATABLE
There are two options when setting ResultSet to be scrollable: TYPE_SCROLL_INSENSITIVE - The result ...
- [转]用python爬虫抓站的一些技巧总结 zz
来源网站:http://www.pythonclub.org/python-network-application/observer-spider 学用python也有3个多月了,用得最多的还是各类爬 ...
- linux 自启动 ,让生活更美好!!
systemctl enable svnserve.service systemctl enable iptables.service systemctl enable firewalld.servi ...
- SQL Server 索引知识-结构,实现
索引的作用毋庸置疑,但他是如何组织,并实现提高语句访问效率的呢?本篇文章为大家做个详细的介绍. 聚集索引架构 B-tree 如图1-1 a.B-tree的结构,叶子节点为数据.数据按照聚集索引键有序排 ...
- JWT能够干什么,不应该干什么?
http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/ At the start of this article ...
- Oracle EBS 查询用户职责
select b.user_name UserID, b.description User_Name, f.RESPONSIBILITY_NAME RESPONSIBILITY from FND_US ...
- Vue2学习笔记:过渡效果css
过渡效果 Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加 entering/leaving 过渡 <!DOCTYPE html> <html ...