【LeetCode】Multiply Strings
来源:互联网 发布:淘宝便宜55寸彩电价格 编辑:程序博客网 时间:2024/06/05 18:53
Multiply Strings
Total Accepted: 9184 Total Submissions: 45371 My Submissions
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.
【解题思路】
1、数学计算的基本思想转为代码实现。
2、num1和num2按位相乘,累加。
3、两数相乘最大位数,是len(num1)+len(num2)
4、注意两者之中出现"000"这种情况的返回值
5、比较考验基本功底,建议练习。
Total Accepted: 9184 Total Submissions: 45371 My Submissions
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.
【解题思路】
1、数学计算的基本思想转为代码实现。
2、num1和num2按位相乘,累加。
3、两数相乘最大位数,是len(num1)+len(num2)
4、注意两者之中出现"000"这种情况的返回值
5、比较考验基本功底,建议练习。
Java AC 476ms
public class Solution { public String multiply(String num1, String num2) {if ("".equals(num1.replace("0", ""))|| "".equals(num2.replace("0", ""))) {return "0";}int len1 = num1.length();int len2 = num2.length();char arrayA[];char arrayB[];arrayA = num1.toCharArray();arrayB = num2.toCharArray();int column = len1 + len2;int array[] = new int[column];int k = column;for (int i = len2 - 1; i >= 0; i--) {k--;int tempCol = k;int tempArr[] = new int[column];int num = arrayB[i] - '0';for (int j = len1 - 1; j >= 0; j--) {tempArr[tempCol] += (arrayA[j] - '0') * num;if (tempArr[tempCol] >= 10) {int mod = tempArr[tempCol] / 10;tempArr[tempCol] %= 10;tempArr[tempCol - 1] += mod;}tempCol--;}array = addNum(tempArr, array, column);}StringBuffer sb = new StringBuffer();int i = 0;if (array[0] == 0) {i = 1;}for (; i < column; i++) {sb.append(array[i]);}return sb.toString();}public int[] addNum(int tempArr[], int array[], int column) {for (int i = column - 1; i >= 0; i--) {array[i] += tempArr[i];if (array[i] >= 10) {array[i] -= 10;array[i - 1] += 1;}}return array;}}Python AC 944ms
class Solution: # @param num1, a string # @param num2, a string # @return a string def multiply(self, num1, num2): if int(num1) == 0 or int(num2) == 0: return '0' len1 = len(num1) len2 = len(num2) col = len1 + len2 list = [0 for i in range(col)] list1 = [int(num1[i]) for i in range(len1)] list2 = [int(num2[i]) for i in range(len2)] k = col for i in range(len2-1, -1, -1): k -= 1 tempCol = k tempList = [0 for l in range(col)] for j in range(len1-1, -1, -1): tempList[tempCol] += list1[j] * list2[i] if tempList[tempCol] >= 10 : mod = tempList[tempCol] / 10 tempList[tempCol] %= 10 tempList[tempCol - 1] += mod tempCol -= 1 list = self.addNum(tempList, list, col) if list[0] == 0: list = list[1:] list = [str(e) for e in list] return "".join(list) def addNum(self, tempList, list, col): for i in range(col-1, -1, -1): list[i] += tempList[i] if(list[i] >= 10): list[i] -= 10 list[i-1] += 1 return list
0 0
- LeetCode: Multiply Strings
- LeetCode Multiply Strings
- LeetCode: Multiply Strings
- [Leetcode] Multiply Strings
- [LeetCode] Multiply Strings
- [Leetcode] Multiply Strings
- LeetCode 45: Multiply Strings
- [LeetCode]Multiply Strings
- [LeetCode] Multiply Strings
- LeetCode-Multiply Strings
- [leetcode] Multiply Strings
- LeetCode - Multiply Strings
- Leetcode: Multiply Strings
- LeetCode题解:Multiply Strings
- leetcode Multiply Strings
- LeetCode Multiply Strings
- LeetCode | Multiply Strings
- [LeetCode] Multiply Strings
- 冒泡排序
- sdv sit svt对应tr点
- iOS多线程编程之NSThread的使用
- x264编码流程分析
- POJ 2349 Arctic Network
- 【LeetCode】Multiply Strings
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- windows.h和winsock2.h解决办法
- js 下载文件?
- Struts2的系统怎么做到同一帐号不能多人登陆?后登陆的可以把先登陆的挤掉?
- 乐视小米陷版权混战 多个业务存在竞争关系是主因
- Linux下java程序启动脚本(可做开机自启动Service脚本)
- OCP 1Z0 053 62
- 2维动态数组