感觉是大数相乘算法里面最能够描述、模拟演算过程的思路

class Solution {
public String multiply(String num1, String num2) {
if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){
return "0";
} int len1 = num1.length();
int len2 = num2.length();
int len = len1+len2;
int[] arr = new int[len];
for(int i=len1-1;i>=0;i--){
for(int j=len2-1;j>=0;j--){
arr[i+j+1] += (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
}
}
for(int i=len-1;i>0;i--){
if(arr[i]>=10){
arr[i-1] += arr[i]/10;
arr[i]%=10;
}
}
int i=0;
if(arr[i] == 0) i++;
String res = "";
while(i<len){
res = res+arr[i++];
}
return res;
}
}

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. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  4. 43. Multiply Strings 字符串相乘

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

  5. Java [Leetcode 43]Multiply Strings

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

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

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

  7. LeetCode(43. Multiply Strings)

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

  8. Multiply Strings 大数相乘 java

    先贴上代码 public String multiply(String num1, String num2) { String str = ""; StringBuffer sb ...

  9. 43. Multiply Strings字符串相乘

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

随机推荐

  1. Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离

    C. Watchmen   time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  2. HDU 多校1.11

  3. 整数快速乘法/快速幂+矩阵快速幂+Strassen算法

    快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c  二.矩 ...

  4. 洛谷——P1088 火星人

    P1088 火星人 题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常 ...

  5. HDU 1754 I Hate It<区间最值 单点修改>

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDOJ 5385 The path

    Dicription You have a connected directed graph.Let $d(x)$ be the length of the shortest path from $1 ...

  7. Python的hashlib

    Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制 ...

  8. 集群Cluster介绍

    来源:http://www.ibm.com/developerworks/cn/linux/cluster/lw-clustering.html简单的说,集群(cluster)就是一组计算机,它们作为 ...

  9. java工具类获取properties文件的配置

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  10. java_hibernate

    入门:http://jingyan.baidu.com/article/cbf0e500965a352eab289368.html 步骤1.查看是否hibernate支持:file-->plug ...