[leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
, also represented as a string.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contain only digits0-9
. - Both
num1
andnum2
do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
题意:
高精度乘法。
Solution1: Math
Do the simulation like how computer will do multiplication operation
1一个char对应1个digit,
digit相乘后,注意其乘积结果可能需要进位
code
/*
Time: O(n^2). We use nested 2 for loop
Space: O(n). We use int[] to save intermedia infor
*/ class Solution {
public String multiply(String num1, String num2) {
if(num1.length()==0 || num2.length()==0) return "0";
int len1 = num1.length();
int len2 = num2.length();
int [] result = new int [len1+len2]; for(int i = len1-1; i>=0; i--){
for(int j = len2-1; j>=0;j--){
int mul = (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
int idx = i+j+1;
// 可能会进位
int carryIdx = i+j;
mul = mul + result[idx];
result[idx] = mul % 10;
result[carryIdx] = result[carryIdx] + mul/10;
}
}
StringBuilder sb = new StringBuilder();
for(int res: result){
if(sb.length()!=0 || res!=0) sb.append(res);
}
return (sb.length() == 0)? "0" : sb.toString();
}
}
[leetcode]43. Multiply Strings高精度乘法的更多相关文章
- leetcode 43. Multiply Strings(高精度乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [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 ...
随机推荐
- GoStudy——Go语言入门第一个事例程序:HelloWorld.go
package main import ( "fmt" ) func main() { fmt.Println("Hello,world!!!--2019年4月1 ...
- Office常用技巧
文章目录 大小写切换 把word里的自动编号转换为真实的文本 大小写切换 word中修改单词/句子的大小写:选中文字,按shift+F3,可在全大写.全小写.首字符大写间切换. 把word里的自动编号 ...
- Linux系统目录权限chmod误操作权限修复方法
Linux中,如果意外误操作将/目录权限批量设置,比如chmod -R 777 / ,系统中的大部分服务以及命令将无法使用,这时候可以通过系统自带的getfacl命令来拷贝和还原系统权限,若是其他系统 ...
- python configparser使用
.ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成. 以 example.ini为例: [DEFAULT] ServerAliveInterval = 45 Co ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- 从Tomcat的处理web请求分析Java的内存模型
Tomcat作为一个java应用,同样是有主线程和子线程的.主线使用while(true)的方式一直循环,等待客户端来连接.一个客户端来了之后,就从线程池中拿一个线程来处理请求,如果没有配置线程池,就 ...
- Gitlab CI 持续集成的完整实践
Gitlab CI 持续集成的完整实践 本着公司团队初创,又在空档期想搞点事情,搭建了私有Gitlab的契机,顺便把持续集成搭建起,实现了对Python服务端代码的单元测试.静态代码分析和接口测试的持 ...
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》
https://blog.csdn.net/c406495762/article/details/60137956 运行平台:Windows Python版本:Python3.x IDE:Sublim ...
- 第26课 可变参数模板(7)_any和variant类的实现
1. any类的实现 (1)any类: ①是一个特殊的,只能容纳一个元素的容器,它可以擦除类型,可以将何任类型的值赋值给它. ②使用时,需要根据实际类型将any对象转换为实际的对象. (2)实现any ...
- 换上 SansForgetica-Regular 字体,增加记忆能力
最近澳大利亚的RMIT(皇家墨尔本理工大学) 搞出来这么个字体,号称能增强记忆,原理是通过难以识别的字体,让人提起精神去识别,从而记忆更深刻. 果断弄了个试试. 安装过程: 下载字体文件 点这里去下载 ...