LeetCode力扣之 Multiply Strings
2018-03-07 20:51
471 查看
Given two non-negative integers
Both
Both
You must not use any built-in BigInteger library or convert the inputs to integer directly.
num1and
num2represented as strings, return the product of
num1and
num2.Note:The length of both
num1and
num2is < 110.
Both
num1and
num2contains only digits
0-9.
Both
num1and
num2does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
/** * Created by lxw, liwei4939@126.com on 2018/3/7. */ public class L043_Multiply_String { public String multiply(String num1, String num2) { int 4000 len1 = num1.length(); int len2 = num2.length(); int[] pos = 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 pos1 = i + j, pos2 = i + j + 1; int sum = mul + pos[pos2]; pos[pos1] += sum / 10; pos[pos2] = sum % 10; } } StringBuilder sb = new StringBuilder(); for (int p : pos){ if (!(sb.length() == 0 && p == 0)){ sb.append(p); } } return sb.length() == 0 ? "0" :sb.toString(); } public static void main(String[] args){ String num1 = "25"; String num2 = "8"; L043_Multiply_String tmp = new L043_Multiply_String(); System.out.println(tmp.multiply(num1, num2)); } }
相关文章推荐
- leetcode(43)Multiply Strings
- LeetCode 43题之 Multiply Strings
- [leetcode] Multiply Strings
- leetcode Multiply Strings 字符串的乘积问题
- [LeetCode]Multiply Strings
- Java [Leetcode 43]Multiply Strings
- [Leetcode]Multiply Strings
- LeetCode 43 Multiply Strings
- [LeetCode] Multiply Strings
- LeetCode Multiply Strings
- LeetCode(43)Multiply Strings
- LeetCode 42 Multiply Strings
- [LeetCode] Multiply Strings
- leetcode之Multiply Strings
- [LeetCode]Multiply Strings
- LeetCode Multiply Strings
- LeetCode: Multiply Strings. Java
- leetcode[43]Multiply Strings
- [Leetcode] Multiply strings 字符串对应数字相乘
- Leetcode Multiply Strings