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.

public class Solution {
//模拟手算乘法
public String multiply(String num1, String num2) {
int n = num2.length();
String[] addS = new String[n];
for(int i = 0; i < n; i++){
addS[i] = multiplyChar(num1, num2.charAt(i), n-1-i);
}
String res = sum(addS);
int p = 0;
while(p < res.length() && res.charAt(p) == '0')
p++;
if(p == res.length())
return "0";
else
return res.substring(p);
} public String multiplyChar(String num, char c, int digits){
int n = num.length();
char[] res = new char[n + 1];
int add = 0;
for(int i = n; i >= 0; i--){
int b = 0;
if(i-1 >= 0)
b = num.charAt(i-1)-'0';
int cur = b*(c-'0')+add;
add = cur / 10;
cur %= 10;
res[i] = (char)(cur+'0');
}
int p = 0;
while(p <= n && res[p] == '0')
p++;
if(p == n + 1)
return "0";
else{
StringBuffer sb = new StringBuffer();
for(int i = 0; i < digits; i++){
sb.append('0');
}
return new String(res, p, n-p+1) + sb.toString();
}
} public String sum(String[] s){
StringBuffer res= new StringBuffer();
int p = 0;
int cur = 0;
int add = 0;
int maxLength = 0;
for(int i = 0; i < s.length; i++){
maxLength = Math.max(maxLength, s[i].length());
}
do{
cur = 0;
for(int i = 0; i < s.length; i++){
if(p < s[i].length())
cur += s[i].charAt(s[i].length()-1-p)-'0';
}
cur += add;
res.append(cur%10);
add = cur / 10;
p++;
}
while(cur != 0 || p < maxLength);
return new String(res.reverse());
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode: Multiply Strings. Java的更多相关文章

  1. LeetCode: Multiply Strings 解题报告

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

  2. [LeetCode] Multiply Strings 字符串相乘

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

  3. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  4. 43. Multiply Strings (JAVA)

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

  5. LeetCode:Multiply Strings

    题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  6. [Leetcode] Multiply strings 字符串对应数字相乘

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

  7. leetcode面试准备:Multiply Strings

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

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

随机推荐

  1. hnnu 11546 Sum of f(x) (求一个数的全部约数和)

    代码: #include<cstdio> #include<cstring> #define N 200000 using namespace std; long long f ...

  2. ANDROID L——Material Design综合应用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Material Design: Material Design是Google推出的一个全 ...

  3. OGEngine教程:声音载入

    以下介绍声音资源从载入到播放的一个流程 首先,我们将须要的音频文件放到assets文件夹下,OGE中SoundRes和MusicRes为我们封装了非常多经常使用的方法,能够用于载入及播放等经常使用功能 ...

  4. SVN无法修改以前提交日志的办法

    一直用SVN作为代码备份,但是今天偶然发现SVN上不能修改意见提交的代码,于是乎开始谷歌了,最后发现只需要在该工程下的hooks目录下放入pre-revprop-change.bat文件即可. pre ...

  5. oracle数据库单个数据文件的大小限制

    之前没有仔细想过这个问题,因为总是不会用到,也没有犯过类似错误. 顺便提一下学习方法吧. 卤肉的学习方法是:常用知识点,熟悉理论并反复做实验,深入理解:不常用的知识点,相关内容都了解大概,遇到问题时想 ...

  6. Android开发人员必知的开发资源

    developer.android.com 官方开发人员网站推荐资源 在动手编写第一个 Android 应用之前,用心读一读 Android Design 章节.尤其是以下的这些文章: Devices ...

  7. hdu4602(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 题意:对于每个数的分解,列出其元素的出现的个数. 1    2   3    4    5 1  ...

  8. C++ Primer中文版(第5版)

    <C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼)    Josee Lajoie(约瑟 拉乔伊)    Barbar ...

  9. HDU 4869 Turn the pokers(推理)

    HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确 ...

  10. linux centos 卸载jdk

    1.先看看OpenJDK的安装包 $ rpm -qa |grep java tzdata-java-2013b-1.el6.noarch java-1.6.0-openjdk-1.6.0.0-1.61 ...