码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 043 Multiply Strings

时间:2015-05-12 01:35:56      阅读:150      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!