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的更多相关文章

  1. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

  2. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  3. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  4. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  5. 043 Multiply Strings 字符串相乘

    给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意:    num1 和 num2 的长度均小于110.    num1 和 num2 均只包含数字 0 ...

  6. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  7. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  9. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. POJ-2777Count Color 线段树+位移

    这道题对于我这样的初学者还是有点难度的不过2遍A了还是很开心,下面说说想法-- Count Color Time Limit: 1000MS Memory Limit: 65536K Total Su ...

  2. Project facet Java version 1.8 is not supported解决记录

    一看知道是因为jdk版本不一致所导致,如何解决? 方法一: 选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Version  方法二: 在 ...

  3. ubuntu设置屏幕亮度

    楼主的本本是acer4750g ,系统是ubuntu kylin 14.04 原 笔记本ubuntu14.04无法调节屏幕亮度 http://my.oschina.net/lhplj/blog/397 ...

  4. 【poj3177】 Redundant Paths

    http://poj.org/problem?id=3177 (题目链接) 题意 给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥. Solution 我们可以发现,用最少的边使得图中没有桥 ...

  5. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  6. linux系统编程----统计一个目录下的普通文件个数

    主要是为了统计linux系统下一个指定目录下面的普通文件个数,运用目录操作的一些函数,配合递归调用来实现该功能. 首先介绍一下函数原型: 打开一个空目录                    DIR ...

  7. 看看这些JavaScript题目你会做吗?

    题目1 咋一看这题目,还以为答案选择B呢,其实正确答案为D,知道原因吗?接着往下看 map对数组的每个元素调用定义的回调函数并返回包含结果的数组,咋一看还以为它会像如下这样执行: function t ...

  8. Jquery-easyUI-datagrid参数之 queryParams

    http://blog.163.com/xpf_designer/blog/static/19213618920117784055668/ Html <div  region="cen ...

  9. jquery------显示加载的js时出现中文乱码解决方法

    方法: 把my.js文件复制出来,用记事本打开,再另存为的时候设置编码格式为utf-8即可

  10. 添加一个txt文件(例如在桌面),利用后台对文件写入内容

    string str = "今天天气好晴朗,处处好风光."; //需要将字符串转化成字节数组 byte[] buffer = Encoding.Default.GetBytes(s ...