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. window.parent ,window.top,window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  2. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

  3. BZOJ1452 [JSOI2009]Count

    Description Input Output Sample Input Sample Output 1 2 HINT 正解:二维树状数组 解题报告: 这是一道送肉题.二维树状数组直接维护每种颜色的 ...

  4. PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法

    逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...

  5. HDU 4901 The Romantic Hero

    The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u D ...

  6. HTTPS 协议降级攻击原理

    0x00 HTTPS 在传统流行的web服务中,由于http协议没有对数据包进行加密,导致http协议下的网络包是明文传输,所以只要攻击者拦截到http协议下的数据包,就能直接窥探这些网络包的数据. ...

  7. Appserv环境下搭建的PHP环境升级PHP版本(支持微信端口以及thinkphp)

      Appserv 2.5.10 升级PHP from version 5.2 to 5.3 最近,在使用Appserv 2.5.10 中的PHP开发微信时,遇到下面错误 Fatal error  C ...

  8. c++ std::string.c_str()

    语法: const char *c_str();搜索 c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...

  9. WinForm AutoComplete 输入提示、自动补全

    一.前言 又临近春节,作为屌丝的我,又要为车票发愁了.记得去年出现了各种12306的插件,最近不忙,于是也着手自己写个抢票插件,当是熟悉一下WinForm吧.小软件还在开发中,待完善后,也写篇博客与大 ...

  10. 去除DedeCms 5.7后台版权广告链接的方法

    织梦DedeCms 5.7后台有很多的织梦官方的广告链接,下面我们来将这些广告去掉吧. 一.去处后台登陆页login.php广告链. 1.找到登录界面模板文件/dede/templets/login. ...