求解大数相乘问题
 
按照上图所示,进行嵌套循环计算。每次计算出两个位置上的数字,并且对其求和进行更新操作。
下面这个讲解很赞!!!
 
 
参考代码: 
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. Linux环境安装PostgreSQL-10.1[转]

    环境及资源 Linux版本:CentOS release 6.5 PostgresSQL版本:postgresql-10.1 PostgreSQL官网下载地址:https://www.postgres ...

  2. java注解自定义使用

    Java提供了4种注解,专门负责新注解的创建: @Target: 表示该注解可以用于什么地方,可能的ElementType参数有:CONSTRUCTOR:构造器的声明FIELD:域声明(包括enum实 ...

  3. windows reload()

    reload() 方法用于重新加载当前文档.如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已改变.如果文档已改变, ...

  4. scala实现相邻两个元素挑换位置的代码,哈哈

    import scala.math._ import breeze.plot._ import breeze.linalg._ import scala.collection.mutable.Arra ...

  5. asp.net导出excel 问题及服务器的部署dcom组件配置

    一.服务器上没有装office 如果要用MS的,这个问题基本不用考虑,只有安装才能解决,没有其它办法! (即使有牛人弄出来 了,估计也是给自己找麻烦) 不过,我只在服务器上装了一个2003精简版, 我 ...

  6. 【转】maven同时使用maven-surefire-report-plugin和maven-surefire-plugin默认将执行两次test

    https://issues.apache.org/jira/browse/SUREFIRE-753 Here the pom.xml snippet how i configured the rep ...

  7. 《HTTP权威指南》学习笔记——HTTP报文

    HTTP报文 HTTP:互联网的信使 HTTP报文:信使用来搬东西的包裹 1.报文流 HTTP报文:HTTP应用程序之间发送的数据块 组成:元信息开头(文本形式,描述报文的内容和含义)+可选的数据部分 ...

  8. vue的手机端框架mint-ui头部header组件实现返回到上一个浏览页面

    <mt-header title="中文号主页" fixed> <router-link to="" slot="left" ...

  9. 磁盘IO概念及优化入门知识

    在数据库优化和存储规划过程中,总会提到IO的一些重要概念,在这里就详细记录一下,对这个概念的熟悉程度也决定了对数据库与存储优化的理解程度,以下这些概念并非权威文档,权威程度肯定就不能说了. 读/写IO ...

  10. Installshield Major upgrade

    Major upagrade: delete old version firstly, then install new version. need to change [product code] ...