LeetCode 43 Multiply Strings(字符串相乘)

package leetcode_50; /***
*
* @author pengfei_zheng
* 实现大数相乘
*/
public class Solution43 {
public static String multiply(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
int []pos = new int [len1+len2];
for(int i = len1-1; i>=0; i--){
for(int j = len2-1; j>=0; j--){
int mul = (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
int p1 = i + j;
int p2 = i + j + 1;
int sum = mul + pos[p2]; pos[p1]+=sum/10;
pos[p2]=sum%10;
}
}
StringBuilder str = new StringBuilder();
for(int p: pos) if(!(str.length() == 0 && p == 0)) str.append(p);
return str.length() == 0 ? "0" : str.toString();
}
public static void main(String[]args){
System.out.println(multiply("987654321","123456789"));
// ans = 121932631112635269
}
}
LeetCode 43 Multiply Strings(字符串相乘)的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
随机推荐
- Chrome Adobe Flash Player 因过期而 阻止
百度搜索重装不管用 作者:胡中元链接:https://www.zhihu.com/question/32223811/answer/60456561来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...
- Jedis客户端操作redis缓存命令详解
1.对value操作的命令 exists(key):确认一个key是否存在 del(key):删除一个key type(key):返回值的类型 keys(pattern):返回满足给定pattern的 ...
- Fedora26 tftp-server设置
安装tftp-server yum install -y tftp-server 启动软件 systemctl start tftp.socket systemctl enable tftp.soc ...
- SharePoint 2013 将HTML文件转换为母版页
内容中包含 base64string 图片造成字符过多,拒绝显示
- Oracle 高级查询
Oracle SQL 一些函数用法 以下sql环境都是在 Oracle 11g/scott完成 Group by 与GROUP BY一起使用的关建字 GROUPING,GROUP SET,ROLLUP ...
- andoid-sdk 安装时出现 Stopping ADB server failed(code -1) 错
出错原因: cmd在path路径找不到adb命令,是因为adb.exe文件存在于android-sdk安装目录platform-tools/子目录下,要将这个路径配置到环境变量里面. 解决方案: 按照 ...
- 修改vs2005,vs2008,vs2010调试默认浏览器
前些日子不小心安装上了一个sogou的浏览器,感觉这个浏览器用起来也算方便,所以就么有卸载,一直就这么用着,但当我用vs来调试web程序的 时候问题出来了,默认的调试浏览器变成了搜狗的浏览器了,我在v ...
- 站长常用的200个js代码 站长常用js代码大全 站长常用js代码集合
站长常用的200个js代码 1.文本框焦点问题 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect: ...
- eclipse中断点不生效
摘录自:http://blog.sina.com.cn/s/blog_496117520100kw6b.html Eclipse下Debug时弹出错误“Unable to install breakp ...
- C#操作共享文件夹
public class NetFileShare { public NetFileShare() { } public static bool connectState(string path) { ...