leetcode -- Multiply Strings
2013-08-05 23:02
381 查看
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.
大整数乘法,一位一位往上乘,注意进位的处理即可。此外,注意0的处理
ref:http://gongxuns.blogspot.com/2013/01/leetcode-multiply-strings.html
Note: The numbers can be arbitrarily large and are non-negative.
大整数乘法,一位一位往上乘,注意进位的处理即可。此外,注意0的处理
public class Solution { public String multiply(String num1, String num2) { // Start typing your Java solution below // DO NOT write main() function int l1 = num1.length(); int l2 = num2.length(); int[] num = new int[l1 + l2 + 1]; for(int i = 0; i <= l2 - 1; i ++){ int carry = 0; int a = num2.charAt(l2 - 1 - i) - '0'; for(int j = 0; j <= l1 - 1; j ++){ int b = num1.charAt(l1 - 1 - j) - '0'; num[i + j] += a * b + carry; carry = num[i + j] / 10; num[i + j] = num[i + j] % 10; } num[i + l1] = carry; } int i = num.length - 1; while(i > 0 && num[i] == 0){ i--; } StringBuilder sb = new StringBuilder(); while(i >= 0){ sb.append(num[i--]); } return sb.toString(); } }
ref:http://gongxuns.blogspot.com/2013/01/leetcode-multiply-strings.html
相关文章推荐
- LeetCode Multiply Strings
- Leetcode之Multiply Strings
- LeetCode 45: Multiply Strings
- Leetcode:43. Multiply Strings
- Multiply Strings leetcode java
- [LeetCode] Multiply Strings 字符串相乘
- [LeetCode] Multiply Strings
- [leetcode] Multiply Strings
- LeetCode-Multiply Strings-字符串乘法-大整数-字符串操作
- LeetCode: Multiply Strings [042]
- leetcode做题总结,题目Multiply Strings 43
- 【leetcode】Multiply Strings
- [LeetCode] Multiply Strings 解题报告
- Leetcode43 Multiply Strings
- leetcode----Multiply Strings
- LeetCode 43 Multiply Strings
- [LeetCode]Multiply Strings
- LeetCode Multiply Strings
- LeetCode: Multiply Strings
- [*leetcode 43] Multiply Strings