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.
解题思路一:
BigInteger!!! JAVA实现如下:
static public String multiply(String num1, String num2) { java.math.BigInteger bi1=java.math.BigInteger.valueOf(0); for(int i=0;i<num1.length();i++){ bi1=bi1.multiply(java.math.BigInteger.valueOf(10)); bi1=bi1.add(java.math.BigInteger.valueOf(num1.charAt(i)-‘0‘)); } java.math.BigInteger bi2=java.math.BigInteger.valueOf(0); for(int i=0;i<num2.length();i++){ bi2=bi2.multiply(java.math.BigInteger.valueOf(10)); bi2=bi2.add(java.math.BigInteger.valueOf(num2.charAt(i)-‘0‘)); } return bi1.multiply(bi2).toString(); }
360 ms Accepted,值得注意的是系统不会自动导入math包,需要在声明时添加,不过使用BigInteger总有种作弊的赶脚
时间: 2024-10-10 14:44:18