标签:leetcode
https://oj.leetcode.com/problems/multiply-strings/
http://blog.csdn.net/linhuanmars/article/details/20967763
import java.math.BigInteger;
public class Solution {
public String multiply(String num1, String num2)
{
// Solution B:
// return multiply_Cheating(num1, num2);
// Solution A:
return multiply_Char(num1, num2);
}
////////////////////////////////
// Solution A:
//
private String multiply_Char(String num1, String num2)
{
if (num1.equals("0") || num2.equals("0"))
return "0";
StringBuilder sb = new StringBuilder();
int num = 0;
for(int i = num1.length() + num2.length() ; i > 0 ; i --)
{
for(int j = Math.min(i - 1, num1.length()) ; j > 0 ; j --)
{
if(i - j <= num2.length())
{
num += (int)(num1.charAt(j - 1) - ‘0‘) *
(int)(num2.charAt(i - 1 - j) - ‘0‘);
}
}
if(i != 1 || num > 0)
sb.append(num % 10);
num = num / 10;
}
return sb.reverse().toString();
}
////////////////////////////////
// Solution B: Cheating
//
private String multiply_Cheating(String num1, String num2)
{
BigInteger a = new BigInteger(num1);
BigInteger b = new BigInteger(num2);
return a.multiply(b).toString();
}
}标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1598594