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 ...
随机推荐
- C#给文件夹添加权限
//==== //添加权限 private void SetAttributes(string folder) { if (folder == "" || !Directory.E ...
- bzoj 1491 floyd
我们用w[i][j]表示i到j的最短路的数量,dis[i][j]表示i到j的最短路,那么我们在floyd的时候,如果dis[i][k]+dis[k][j]==dis[i][j],根据乘法原理我们就w[ ...
- HDU #3333
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descript ...
- javascript-如何判断一个对象为数组
Q:如何判断一个对象是否为数组? A1:判断对象的constructor是否指向Array, 接着判断对应的特殊属性,如length,splice之类.这个很容易冒充. A2:使用instanceof ...
- Looper
/** * Class used to run a message loop for a thread. Threads by default do * not have a message loop ...
- redis设置密码和主从复制
redis设置连接密码: 因为redis的速度相当的快,在一台比较好的服务器下,每秒可进行150K次密码尝试,所以要设置个非常强大的密码. 设置方式:修改配置文件redis.conf #require ...
- Redis 数据库
Redis 服务器 Remote Dictionay Server Redis是一个key-value持久化产品,通常被称为数据结构服务器. Redis的key是string类型: ...
- MSF溢出实战教程
1. 进入终端,开启MSF相关服务 2. 连接数据库 3. 主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4. 开始溢出 溢出成功的话 sessions -l 查看 ...
- php-fpm.conf两个至关重要的参数
这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...
- 如果jsp提交到action为空指针的话
很严重的一点:表单<form>有没有添加一个method="post",如果表单的这个没有写,肯定是空指针, 哎,被这个坑爹的代码,查了好久,特此记录下