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. linux下的块设备驱动(二)

    上一章主要讲了请求队列的一系列问题.下面主要说一下请求函数.首先来说一下硬盘类块设备的请求函数. 请求函数可以在没有完成请求队列的中的所有请求的情况下就返回,也可以在一个请求都不完成的情况下就返回. ...

  2. java thread reuse(good)

    I have always read that creating threads is expensive. I also know that you cannot rerun a thread. I ...

  3. hdu 3290 (简单dfs)

    题意:没有儿子的节点所结苹果数是节点的编号,有儿子的所结苹果是儿子数量(k+1)/2个,求跟节点的苹果数 直接递归一下,先求出所有儿子的苹果树,在排序,,刚开始以为1就是根节点,根节点不确定,, #i ...

  4. git/github初级运用自如 (转)

    三 . 设置用户信息 这一步不是很重要,貌似不设置也行,但github官方步骤中有,所以这里也提一下. 在git中设置用户名,邮箱 $ git config --global user.name &q ...

  5. Python 实现的下载op海贼王网的图片(网络爬虫)

    没得事就爬一下我喜欢的海贼王上的图片 须要在d盘下建立一个imgcache目录 # -*- coding: utf-8 -*- import urllib import urllib2 import ...

  6. Unity学习笔记(二)——第一个Unity项目Hello Unity

    保留版权,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/9318811 在这一篇文章里,参照宣雨松的<Unity 3D游戏开发& ...

  7. MFC模态对话框的消息循环

    MFC模态对话框的消息循环 单线程程序, 当主窗口响应函数中弹出模态对话框时,为什么主窗口响应函数可能照常工作? 当弹出模态对话框时,线程的消息循环无法返回,父窗口的事件本应没人处理,应该处于卡死状态 ...

  8. git配置流程

    写的比较简略,主要是记录一下,以后配置别的机器的时候看一下,仅供参考. 1 安装 命令依赖包 sudo apt-get install git-core git-gui git-doc 2 设置SSH ...

  9. C++操作符operator的另一种用法

    http://blog.csdn.net/memewry/article/details/7833314 参考地址 今天在程序员面试宝典上看到这样一道题目:  A C++ developer want ...

  10. jQuery中的getJSON()

    json文件是一种轻量级的数据交互格式.一般在jQuery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:json文件地址 data:可选 ...