leetcode 43. Multiply Strings(高精度乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note:
- The numbers can be arbitrarily large and are non-negative.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
高精度乘法。需要注意的是char表示的数字和int对应的数字之间的转化。
class Solution {
public:
string multiply(string num1, string num2) {
int l1=num1.length(),l2=num2.length();
if(l1==||l2==){
string ans="";
return ans;
}
string ans(l1+l2,'');
for(int i=l1-;i>=;i--){
int add=;
for(int j=l2-;j>=;j--){
int t=(ans[i+j+]-'')+(num1[i]-'')*(num2[j]-'')+add;
ans[i+j+]=t%+'';
add=t/;
}
ans[i]=add+'';
}
size_t s0 = ans.find_first_not_of('');
if(s0!=string::npos){
return ans.substr(s0);
}
return "";
}
};
leetcode 43. Multiply Strings(高精度乘法)的更多相关文章
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 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 ...
- 43. Multiply Strings (大数乘法)
DescriptionHintsSubmissionsDiscussSolution Pick One Given two non-negative integers num1 and num2 ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
随机推荐
- 双十一前4小时,CentOS 6.5server启动错误排查
11月10日晚上8点多.眼看要到双十一了... 但我要说的这段经历却和双十一毫无关系.哈哈. 这天准备向CentOS6.5server的svn上传一些文件,结果开机启动时,却出现了以下的界面: 这是肿 ...
- 基于faro SDK 读取fls原始文件
#define _SCL_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <iostream> //#include ...
- Xenomai PC开发环境
这两天总在纠结编译一个PC机上的Xenomai开发环境,选择编译器.kernel版本和IPIP版本,但是今天忽然想到,上位机只是个开发环境,只要能编译.能运行就可以了,实时性根本不是关注的东西.而Xe ...
- SQL注入基础入门
一般的WEB架构 SQL注入成因: 用户开启浏览器并连接http://www.xxx.com.位于逻辑层的Web服务器从文件系统中加载脚本将其传递给脚本引擎,脚本引擎负责解析并执行脚本. 脚本使用数据 ...
- CxImage新手教程,图文并茂
作为一个游戏client程序猿,须要对图像处理有一定的知识. CxImage是C++实现的功能强大的.能处理多种文件格式的图像管理类.它可以简单高速的实现图像的导入.保存.显示和变换. 同一时候又具有 ...
- python学习(七)字典学习
#!/usr/bin/python # 字典 # 当时学java的时候, 语言基础就学了好久, 然后是各种API, 最后才是集合 # 键值对, 可变 # 1. 映射操作 D = {'food' : ' ...
- 安装部署zookeeper集群
实验说明: 三台虚拟机做zookeeper集群,集群个数最好是奇数个,原理详见zookeeper 详解 安装zookeeper 请确保jdk 已安装好,否则无法启动 三台虚拟机IP分别为:192. ...
- centOS解决乱码问题
问题描述:输入javac出现乱码,部分字符不能显示解决方法 echo 'export LANG=en_US.UTF-8' >> ~/.bashrc
- MongoDB的对象的创建
package com.voice.db; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mon ...
- 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称
说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...