LeetCode: Multiply Strings. Java
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的更多相关文章
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [leetcode]Multiply Strings @ Python
原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...
- 43. Multiply Strings (JAVA)
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode:Multiply Strings
题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- 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) ...
- LeetCode 43. 字符串相乘(Multiply Strings)
43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...
随机推荐
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个n个数的序列,当中组成的数仅仅有0-n,我们能够进行这么一种操作:把第一个数移到最 ...
- 用Qt开发Web和本地混合的应用
QtWebkit 模块使得Qt widget能够通过HTML的object标签嵌入到web页面中,并通过JavaScript代码进行访问,而Qt对象也能相应的访问web页面元素. 将Qt对象插入到we ...
- ThinkPHP框架视图详细介绍 View 视图--模板(九)
原文:ThinkPHP框架视图详细介绍 View 视图--模板(九) 视图也是ThinkPHP使用的核心部分: 一.模板的使用 a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和 ...
- thinkPHP四种URL访问方式(二)
原文:thinkPHP四种URL访问方式(二) 四.url的4种访问方式 1.PATHINFO 模式 -- (重点) http://域名/项目名/入口文件/模块名/方法名/键1/值1/键2/ ...
- POJ 3040 Allowance 贪心
这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...
- The usage of V$PGA_TARGET_ADVICE
Oracle 10g 给出了一系列的自动优化的建议,告诉我们PGA分配多大能给系统带来最大的性能?V$PGA_TARGET_ADVICE视图给出了很好的“预测”! 看一下这个视图能给我们带来什么样的信 ...
- 为了树莓派IIraspberrypi安装emacs+ecb+cedet+session+color-theme+cscope+linum
类似这篇文章写的不多,为了避免以后大家转来转去而忽略了写文章的时间,这些特别加上是2014年6月28日,省的对不上一些软件的版本号(下文中有些"最新"的说法就相应这个时间).假设转 ...
- MongoDB在实际项目
MongoDB在实际项目中的使用 MongoDB简介 MongoDB是近些年来流行起来的NoSql的代表,和传统数据库最大的区别是支持文档型数据库.当然,现在的一些数据库通过自定义复合类型,可变长 ...
- projecteuler---->problem=9----Special Pythagorean triplet
title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...
- 做SEO所要具备的四种能力
1,不为失败找借口 既然我们选择了做SEO,那么发生网站被降权.被K是常常的事.当这样的情况发生时,大部分站长首先将责任推给百度机制,由于百度更新算法调整遭降权,不是由于他们的优化没有 ...