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

    Linq中string转int的方法   在做批量删除时,需把一串id值所对应的数据删除,调试出现问题: Linq语句中如果使用ToString()进行类型转换,编译时不会报错,但执行时会出现如下错误 ...

  2. 第一個shell腳本

    #!/bin/bash echo "Hello World !" 運行

  3. Css 去除浮动

    清除浮动的方法 清除浮动方法大致有两类,一类是clear:both | left | right ,另一类则是创建BFC,细分又可以分为多种. 通过在浮动元素末尾添加一个空的标签例如并设置样式为cle ...

  4. 如何设置Jquery UI Menu 菜单为横向展示

    Jquery UI Menu 默认是纵向展示的.Jquey UI  Menu 设置API,http://api.jqueryui.com/menu/#option-position 修改对应的CSS可 ...

  5. 在C++中实现不可继承的类

    逛下bbs,“在C++中实现不可继承的类”,瞒有意思的. class NoInherite { friend class Seal; private: NoInherite(void) {} ~NoI ...

  6. Java编程思想学习笔记——注解

    前言 在Android开发的过程中,我们为了减少重复代码的编写,会使用类似ButterKnife,AndroidAnnotations 这类依赖注解库.代码示例如下: //不使用 Button btn ...

  7. WebGL Matrix4(4*4矩阵库)

    Matrix4是由<<WebGL编程指南>>作者写的提供WebGL的4*4矩阵操作的方法库,简化我们编写的代码.源代码共享地址,点击链接:Matrix4源代码. 下面罗列了Ma ...

  8. python 捕捉错误,exception,traceback和sys.exc_info()比较

    import traceback,sys import requests try : requests.get('dsdsd') ##故意让他出错 except Exception,e: print ...

  9. NetBpm如何指定下一流程处理人(8)

    NETBPM如何指定下一流程处理人 本着“软件以应用为本”(潘加宇老师对我的影响在这一点上很深.)的原则,我为ERP搭建了一个用NETBPM作的支持网站,想着能够看着软件得以应用,自是非常高兴. 不过 ...

  10. ios开发之--随机背景颜色

    记录个随机背景颜色的方法: + (UIColor*) randomColor{ NSInteger r = arc4random() % ; NSInteger g = arc4random() % ...