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. RBAC用户角色权限设计方案

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用 户-角色 ...

  2. 齐博软件(地方门户系统) 文件加密破解工具

    原文:齐博软件(地方门户系统) 文件加密破解工具 本程序为针对"齐博软件地方门户系统5.0官方原版"的破解工具,一个垃圾系统居然弄出这么恶心的加密方式,有个鸟用!以后见一个破一个! ...

  3. UIImagePickerController本地化控件文字

    在使用UIImagePickerController时候,你会发如今选择照片或者拍照的时候,界面的很多控件都是英文的,比方"Cancel","Choose"等. ...

  4. 微信公众平台PHP开发

    p=932" style="color: rgb(255, 153, 0); text-decoration: none;">微信公众平台PHP开发 2013.05 ...

  5. form表单中的 action=./?> 是什么意思

    ./代表当前目录,?代表查询字符串为空 action="" //一般可以为空的,这里的双引号都要有的,表示提单提交给自己(也就是当前页处理)action="a.php&q ...

  6. CC 3-Palindromes(manacher)

    传送门:3-Palindromes 题意:求为回文串且能整除3且不前导0的子串个数. 分析:由 manacher算法O(N)可算出以i为坐标的最长为p[i]回文子串,且Si-k,Si-k+1..... ...

  7. Unix Domain Socket 域套接字实现

    主要注意流程: STREAM SOCKET: Server :  socket() --->  bind() ---> listen()  ---> accept() Client: ...

  8. hdu3853(概率dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有一个人被困在一个 R*C(2<=R,C<=1000) 的迷宫中,起初他在 ( ...

  9. python列表和QVariant

    pyqt中.要给QAbstractTableModel的setData函数传递一个list參数: [20,'00:00:19'] 涉及到QVariant和list的转换. 能够使用QVariant类中 ...

  10. Phalcon之 表单(Forms)

    Phalcon中提供了 Phalcon\Forms组件以方便开发人员创建和维护应用中的表单. 以下的样例中展示了主要的用法: <?php use Phalcon\Forms\Form, Phal ...