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 ...
随机推荐
- 腾讯QQ积分CSRF导致积分任意挥霍(我的积分为什么少了)
触发点:http://jifen.qq.com/html5/index.html?ADTAG=JIFEN.MART.INDEX 随意兑换一个商品: 因为刚才我已经兑换过了,所以积分没有了.. 没关系, ...
- Linux服务器安装svn
云安装 yum install subversion 配置 1.配置仓库 [root@localhost /]# cd /home [root@localhost home]# mkdir svn [ ...
- Node.js静态文件服务器实战[转]
p.s. 在下面这篇文章的指导下,做了一个静态文件服务器,见:https://github.com/walkerwzy/node_static_server ==== 这是一篇阐述得比较详细的文章,从 ...
- mint-ui loadmore组件注意问题
loadTop(){ this.$store.dispatch('getNewsList',{channelId:this.id,page:0,size:this.size}); this.$refs ...
- Webkit内核探究【1】——Webkit简介
出处:http://www.cnblogs.com/jyli/archive/2010/01/31/1660355.html作者:李嘉昱 研究Webkit内核已经有一段时间了,在这期间我花了很多时间去 ...
- Tensorflow同时加载使用多个模型
在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...
- 用VS2012不能打开VS2010的项目
应该是装过sql2012或者sql2008,自带的那个visual studio,你可以直接在外面打开VS2012,然后用里面的文件去打开项目.而不要直接双击那个解决方案的文件.
- [原]单片机/Stm32教程
1 http://www.amobbs.com/forum.php?mod=viewthread&tid=4462962 2.http://bbs.21ic.com/forum.php?mod ...
- linq to xml 增删查改
一.XML基本概述 XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境 ...
- 【高级算法】遗传算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46910079 1 SAT问题描写叙述 命题逻辑中合取范式 (CNF) 的可满足性问 ...