43Multiply Strings
来源:互联网 发布:php shell exec 编辑:程序博客网 时间:2024/06/05 18:42
题目链接:https://leetcode.com/problems/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.
解题思路:
这个解题思路还是在做考研机试时学会的,运行时间相对较慢。但算法思路相对简单。
把两个大整数乘法拆分为:一个大整数乘以一个整数,两个大整数求和两个部分。
举例:123 * 234 = 28782
123 * 4 = 492
123 * 3 = 369
123 * 2 = 246
把上述乘积错位相加。
246 369 + 492 28782
代码如下:
public class Solution { public String multiply(String num1, String num2) { if(num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0) return null; if(num1.equals("0") || num2.equals("0")) return "0"; String preSum = "0"; for(int i = num2.length() - 1; i >= 0; i --) { String mul = multiply2(num1, num2.charAt(i) - '0'); StringBuilder sb = new StringBuilder(mul); for(int j = 0; j < num2.length() - i - 1; j ++) sb.append("0"); preSum = sum(preSum, sb.toString()); } return preSum; } String sum(String a, String b) { StringBuilder sb = new StringBuilder(); int i = a.length() - 1; int j = b.length() - 1; int carry = 0; while(i >= 0 && j >= 0) { int sum = (a.charAt(i) - '0') + (b.charAt(j) - '0') + carry; carry = sum / 10; sb.append(sum % 10); i --; j --; } while(i >= 0) { int sum = (a.charAt(i) - '0') + carry; carry = sum / 10; sb.append(sum % 10); i --; } while(j >= 0) { int sum = (b.charAt(j) - '0') + carry; carry = sum / 10; sb.append(sum % 10); j --; } if(carry != 0) sb.append(carry); return sb.reverse().toString(); } String multiply2(String num, int k) { StringBuilder sb = new StringBuilder(); int carry = 0; for(int i = num.length() - 1; i >= 0; i --) { int n = k * (num.charAt(i) - '0') + carry; carry = n / 10; sb.append(n % 10); } if(carry != 0) sb.append(carry); return sb.reverse().toString(); }}
311 / 311 test cases passed.Status: AcceptedRuntime: 368 ms
注:另一种思路很简便,从网上看到的
基本思路是和加法运算还是近似的,只是进位和结果长度复杂一些。我们仍然是从低位到高位对每一位进行计算,假设第一个数长度是n,第二个数长度是m,我们知道结果长度为m+n或者m+n-1(没有进位的情况)。对于某一位i,要计算这个位上的数字,我们需要对所有能组合出这一位结果的位进行乘法,即第1位和第i位,第2位和第i-1位,… ,然后累加起来,最后我们取个位上的数值,然后剩下的作为进位放到下一轮循环中。这个算法两层循环,每层循环次数是O(m+n),所以时间复杂度是O((m+n)^2)。算法中不需要额外空间,只需要维护一个进位变量即可,所以空间复杂度是O(1)。
参考链接:http://blog.csdn.net/linhuanmars/article/details/20967763
0 0
- LeetCode(43) Multiply Strings
- [leetcode 43] Multiply Strings
- [LeetCode 43]Multiply Strings
- leetcode || 43、Multiply Strings
- leetcode-43 Multiply Strings
- Multiply Strings leetcode 43
- leetcode 43: Multiply Strings
- Leetcode 43 Multiply Strings
- 43Multiply Strings
- leetcode 43 : Multiply Strings
- Leetcode #43 Multiply Strings
- leetcode 43: Multiply Strings
- LeetCode(43) Multiply Strings
- LeetCode-43 Multiply Strings
- Leetcode#43||Multiply Strings
- [Leetcode]#43 Multiply Strings
- 43 Multiply Strings
- 43 Multiply Strings
- 不同钱币组合方案问题
- DSP 6678 大小端转换的问题
- 欢迎使用CSDN-markdown编辑器
- OLE、ActiveX、COM、ATL联系与区别
- 细说Android drawable
- 43Multiply Strings
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- com的主要接口介绍
- 自定义delegate模式
- 自定义View构造函数参数理解
- Android SDK Manager中各个文件的作用-总结
- 亿级Web系统搭建——单机到分布式集群
- 在MyEclipse中快捷键的使用
- MBR&GPT