求解大数相乘问题
 
按照上图所示,进行嵌套循环计算。每次计算出两个位置上的数字,并且对其求和进行更新操作。
下面这个讲解很赞!!!
 
 
参考代码: 
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(字符串相乘)的更多相关文章

  1. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  2. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  3. 43. Multiply Strings 字符串相乘

    1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...

  4. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

  5. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

  6. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  7. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  9. [leetcode]43. Multiply Strings高精度乘法

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

随机推荐

  1. win用VNC远程Ubuntu教程

    转载:https://blog.csdn.net/jiangchao3392/article/details/73251175 1.安装Xrdp Windows远程桌面使用的是RDP协议,所以ubun ...

  2. spark shell学习笔记

    http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html

  3. 【WP8】WP8调用官方API使用LED灯

    在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...

  4. 动态为页面添加CSS样式文件引用

    动态为页面添加CSS样式文件引用: if (document.createStyleSheet) { //IE document.createStyleSheet("./Themes/Def ...

  5. koa中上传文件到阿里云oss实现点击在线预览和下载

    比较好的在线预览的方法: 跳转一个新的页面,里面放一个iframe标签,或者object标签 <iframe src="xxx"></iframe> < ...

  6. Java 11正式发布,这几个逆天新特性教你写出更牛逼的代码

    就在前段时间,Oracle 官方宣布 Java 11 (18.9 LTS) 正式发布,可在生产环境中使用! 这无疑对我们来说是一大好的消息.作为一名java开发者来说,虽然又要去学习和了解java11 ...

  7. winform下 PictureBox 显示网络图片

    Image pic = new Image.FromStream(WebRequest.Create("http://x.com/x.jpg").GetResponse().Get ...

  8. mysql中参数low_case_table_name的使用?不同参数值的设置有什么影响?

    需求描述: 今天一个同事问,在mysql中,默认的表名是大小写区分的吗,默认是什么设置, 如果要设置成大小写不区分的改怎么设置,是否需要进行重启.然后就进行了查询, 对于lower_case_tabl ...

  9. war内部结构

    war index.html(非必须) WEB-INF classes (java编译之后的class文件) lib(jar文件) web.xml(war包描述文件) subdirectories[可 ...

  10. ASP.NET Web API 使用Swagger使用笔记

    https://www.cnblogs.com/lhbshg/p/8711604.html 最近换了工作,其中Webapi这块没有文档,之前有了解过Swagger借此机会好好整理下常用的地方分享给有需 ...