当前位置:网站首页>力扣43字符串相乘笔记
力扣43字符串相乘笔记
2022-07-17 07:33:00 【雷霆小嘎巴】
力扣43.字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。
示例 1:
输入: num1 = "2", num2 = "3"
输出: "6"
示例 2:
输入: num1 = "123", num2 = "456"
输出: "56088"
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/multiply-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
刚开始时,是我想简单了,没有考虑到整数范围会溢出,导致最后有一堆特别长的相乘根本过不去
class Solution { public String multiply(String num1, String num2) { long sum1 = si(num1); long sum2 = si(num2); long x = sum1*sum2; String s = x+""; return s; } public static int si(String s){ int sum = 0; for(int i=0;i<s.length();i++){ int a = s.charAt(i)-48; sum = a+sum*10; } return sum; } }这里数据类型由一开始的int到long,最后还是过不去,后面的例子长度超过了long的范围,可是题目要求不能用内置的BigInteger库,所以这个方法最后废弃了。
于是开始看题解,发现这题远比想的要难,找到一个最优解---优化竖式法,这里面体现了数学思想。
该算法是通过两数相乘时,乘数某位与被乘数某位相乘,与产生结果的位置的规律来完成。具体规律如下:
乘数 num1 位数为 M,被乘数 num2 位数为 N, num1 x num2 结果 res 最大总位数为 M+N
num1[i] x num2[j] 的结果为 tmp(位数为两位,"0x","xy"的形式),其第一位位于 res[i+j],第二位位于 res[i+j+1]。这里附上题解带的图
相同位数的数相乘,其结果的最大位数只能是这一位或这一位并包括再进一位,同理, 也就是上面的i+j或i+j+1
代码
class Solution { public String multiply(String num1, String num2) { if (num1.equals("0") || num2.equals("0")) { return "0"; }//先判断一下,如果其中有一个为0,那么可以不用管了,直接退出 int[] res = new int[num1.length() + num2.length()]; //上面我们已经得知最大总位数是M+N,即两个数字位数之和,这里就直接明确了新数组的位数 for (int i = num1.length() - 1; i >= 0; i--) { //从个位数开始遍历 int n1 = num1.charAt(i) - '0';//每个字符要减去’0‘才能是实际数字大小 for (int j = num2.length() - 1; j >= 0; j--) { int n2 = num2.charAt(j) - '0'; int sum = (res[i + j + 1] + n1 * n2); /*sum为第一个数的一位与第二个数的一位相乘的结果再加上上一次的乘积的余数*/ res[i + j + 1] = sum % 10; res[i + j] += sum / 10;//新数组每一位便是每一次sum的最高位 } } StringBuilder result = new StringBuilder(); for (int i = 0; i < res.length; i++) { if (i == 0 && res[i] == 0) continue; result.append(res[i]); //将结果中每一位的元素添加到新数组中 } return result.toString(); } }
边栏推荐
- QT related problems encountered when writing code
- JS学习笔记06-08:数组的遍历以及数组的四个方法
- A small program about daily habit clock in
- 5G正当时,无人驾驶未来将驶向何方?
- Unity: window size adaptation when running on the browser after webgl Publishing
- Redis 概述安装
- Talk about distributed locks
- 美国压力激增,TikTok 更换全球安全主管
- 60. Initial knowledge of wsgiref handwritten web framework +jinja2 module
- 【flask入门系列】异常处理
猜你喜欢

MySQL 2502 2503 error

Application of SCA on devsecops platform

Super dry! Thoroughly understand golang memory management and garbage collection

演示集合注入

ansible自动化运维详解(四)ansible中playbook的编写使用、执行命令及实例演示

Real case: how to check the soaring usage of CPU after the system goes online?

Talk about distributed locks

Detailed explanation of ansible automatic operation and maintenance (IV) preparation, use, command execution and example demonstration of playbook in ansible

Bean、

Enjoy JVM -- knowledge about GC garbage collection
随机推荐
812. Maximum triangle area
行为型模式之策略模式
Set settings in vscode json
[characteristic Engineering]
畅玩JVM——关于GC垃圾回收必须要掌握的知识
How does the V8 engine recycle garbage memory?
Openpyxl copy sheet pages across workbooks
深度学习之 7 深度前馈网络2
Redis新数据类型——Bitmaps
RestTemplate
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
#yyds干货盘点#Cross-origin 跨域请求
依赖注入方式
No module named ‘yaml‘ 解决办法
leetcode:287. Find the repetition number [fast and slow pointer board]
SCA在得物DevSecOps平台上应用
【Kernel】驱动开发学习之字符设备
Redis介绍
5g at that time, where will driverless driving go in the future?
Obtain the home location through IP
