Java for LeetCode 043 Multiply Strings
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实现如下:
import java.math.BigInteger; public class Solution {
static public String multiply(String num1, String num2) {
BigInteger bi1=BigInteger.valueOf(0);
for(int i=0;i<num1.length();i++){
bi1=bi1.multiply(BigInteger.valueOf(10));
bi1=bi1.add(BigInteger.valueOf(num1.charAt(i)-'0'));
}
BigInteger bi2=BigInteger.valueOf(0);
for(int i=0;i<num2.length();i++){
bi2=bi2.multiply(BigInteger.valueOf(10));
bi2=bi2.add(BigInteger.valueOf(num2.charAt(i)-'0'));
}
return bi1.multiply(bi2).toString();
}
}
360 ms Accepted,值得注意的是系统不会自动导入math包,需要在声明时添加。
解题思路二:使用BigInteger总有种作弊的赶脚,题目的真正意思是让我们用加法模拟乘法的过程,JAVA实现如下:
static public String multiply(String num1, String num2) {
StringBuilder result=new StringBuilder();
for(int i=0;i<num1.length()+num2.length();i++)
result.append('0');
for (int i = num1.length()-1; i >=0; i--)
for (int j = num2.length()-1; j>=0; j--) {
int tmp = (result.charAt(i+j+1) - '0') + (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
result.replace(i+j+1, i+j+2, ""+tmp % 10);
for(int k=i+j;;k--){
tmp=result.charAt(k)- '0' + tmp / 10;
result.replace(k, k+1,""+ tmp%10);
if(tmp<10)
break;
}
}
for (int i = 0; i <result.length(); i++)
if(result.charAt(i)!='0')
return result.substring(i, result.length());
return "0";
}
Java for LeetCode 043 Multiply Strings的更多相关文章
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- 043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意: num1 和 num2 的长度均小于110. num1 和 num2 均只包含数字 0 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- Blog Explanation
有疑问或blog中说明错误的欢迎评论或联系QQ:30056882,邮箱BLADEVIL@outlook.com.本人水平不高,欢迎讨论问题. blog中所有随笔均为原创,转载请注明来源. (已退役.)
- 关于clonezilla
Clonezilla 是一个很好的系统克隆工具,它可以说是吸取了 Norton Ghost 和 Partition Image 的优点.即不仅支持对整个系统进行克隆,而且也可以克隆单个的分区,这种灵活 ...
- wifi与wimax
这几天在看文献中看到802.11a,802.11n和802.16e这几种无信通信协议标准,在网上查了相关资料后,看到有个帖子总结得不错,故将其转载过来. 转:http://blog.csdn.net/ ...
- codeforce 626E(二分)
E. Simple Skewness time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- c++标准库和stl关系
C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...
- Linux目录结构及常用命令(转载)
一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/bin,或/usr/sbin目录下吗?例如,less命令位于/usr/bin目录下.为什么没在/bin中,或 ...
- Java,PostgreSQL时间范围查询
遇到一坑:对于如下代码 select * from order_mileagefuel where date > '2015-11-1' and date< '2015-11-5' 在Po ...
- Jenkins 搭建U3D自动发布 Android
工具 [u3d相关的PostProcessBuildPlayer,PerformBuild.cs] 1.Jenkins 开源包 Java -jar jenkins.war,参考链接 http://w ...
- iconv命令详解
功能] 对于给定文件把它的内容从一种编码转换成另一种编码. [描述] -f encoding :把字符从encoding编码开始转换. -t encoding :把字符转换到encoding编码. ...
- serialization机制
首先说明一下序列化的知识: java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输.或者持久化存储到数据库或文件系统中:然 ...