首页 > 编程语言 > 详细

Java for LeetCode 043 Multiply Strings

时间:2015-05-12 01:35:31      阅读:140      评论:0      收藏:0      [点我收藏+]

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总有种作弊的赶脚

Java for LeetCode 043 Multiply Strings

原文:http://www.cnblogs.com/tonyluis/p/4496115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!