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. [Windows Phone学习笔记]UserControl的使用

    UserControl的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑, ...

  2. 浅谈 PHP 神盾的解密过程

    原文:浅谈 PHP 神盾的解密过程 前些日子一个朋友丢了个shell给我,让我帮忙解密,打开源码看了下写着是 “神盾加密” , 牛逼闪闪的样子.百度下发现神盾是个很古老的东西,最后一次更新是在 201 ...

  3. android--手机桌面添加网址链接图标(解决方式)

    这样的做法最普遍最简单: 1.新建一个android空项目: 2.在drawable文件夹下加入图标文件,如icon.png:在values文件夹下的strings.xml文件里添加名称.如websi ...

  4. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  5. UVA796- Critical Links(无向图中的桥梁)

    题目链接 题意: 给出一个无向图,按顺序输出桥 思路:求出全部的桥,然后按顺序输出就可以 代码: #include <iostream> #include <cstdio> # ...

  6. File already exists: filesystem '/path/file', transaction svn常见错误解决方法

    前言 多人任务基本都会用到SVN,于是提交的时候如果不先更新在提交或者操作顺序不对,会经常出现错误,其中File already exists: filesystem这个就是个常见问题,上网找了半天没 ...

  7. 标准输入的原理:cin与scanf

    1.cin 该方法 1)假设cin读取整数.会自己主动忽略换行和空格.遇到文件结束标记.cin  >> a返回的数false     int a;     while(cin >&g ...

  8. 伤不起的戴尔台式机XPS8700脆弱的蓝牙

    http://en.community.dell.com/support-forums/desktop/f/3514/t/19520747.aspx 1.报价仅仅包含主机,并且不带音响(speaker ...

  9. poj1182(并查集)

    题目链接 分析:根据分析,关系的递推满足由[a,b]~[b,c]得:[a,c]=([a,b]+[b,c])%3;[a,d]=([a,b]+[b,c]+[c,d])%3.由rank数组表示关系 0 -  ...

  10. TreeSet排序

    TreeSet的排序能够通过两种方法来实现: 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比較器进行排序. 2. ...