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 ...
随机推荐
- 【LightOJ 1422】Halloween Costumes(区间DP)
题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...
- github 建立博客
Last login: Wed Jan 27 20:33:21 on console liukun-MBP:~ kamil$ cd ~/.ssh/ liukun-MBP:.ssh kamil$ ls ...
- Nginx 配置文件详解
user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error.log crit; #错误 ...
- shell中条件判断if中的-z到-d的意思
shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 ...
- ArrayList与LinkedList区别
ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个 ...
- python之字符聊天小工具
server side: # coding: gb2312#socket server端#获取socket构造及常量from socket import *#''代表服务器为localhostmyHo ...
- Jquery 鼠标事件解析
1 mouseover与mousemove的区别: mouseover是当鼠标移动到对象时产生,只产生一次,这时如果继续在对象上移动,不会再产生mouseover事件,而是mousemove事件,mo ...
- struts2基本配置
struts.xml 放在src目录下 <?xml version="1.0" encoding="UTF-8"?> <struts> ...
- Bootstrap幻灯轮播如何支持触屏左右滑动手势?
最近ytkah在学习用bootstrap搭建网站,Bootstrap能自适应pc端和手机端,并且移动设备优先,适合现如今移动营销.bootstrap是封装好的框架,需要某些功能只需调用相应的组件就可以 ...
- 用WP_Query自定义WordPress 主循环
我们知道操作 WordPress 主循环(WordPress Loop)最容易的方法是使用 query_posts 函数. 但是使用 query_posts 直接修改 WordPress 默认的主循环 ...