先贴上代码

 public String multiply(String num1, String num2) {
String str = "";
StringBuffer sb = new StringBuffer(num1);
num1 = sb.reverse().toString();
sb = new StringBuffer(num2);
num2 = sb.reverse().toString();
int[] res = new int[num1.length() + num2.length()];
for (int i = 0; i < num1.length(); i++) {
for (int j = 0; j < num2.length(); j++) {
res[i + j] += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
}
}
int k = 0, contribute = 0;
while (k < res.length || contribute > 0) {
int tmp = k<res.length?res[k]:0;
tmp = contribute+tmp;
contribute = tmp/10;
str += tmp%10;
k++;
}
sb = new StringBuffer(str);
str = sb.reverse().toString();
int i = 0;
while(i<str.length()&&str.charAt(i)=='0')
i++;
if(i<str.length()){
str = str.substring(i);
}else{
str = "0";
}
return str;
}

思路如下:

模拟竖式相乘

1、转换并反转,字序反转;
2、逐位相乘,结果存放在res[i+j]中;
3、处理进位;
4、转换并反转,将计算结果转换为字符串并反转。
5、消除多余的0;

两数相乘,结果的长度一定不大于乘数和被乘数的长度之和。

上述也可以用直接用java的BigInteger类实现

public static void main(String[] args) {
String a = "98989898989898956898", b = "989892551548781251323265615150";
BigInteger aa = new BigInteger(a);
BigInteger bb = new BigInteger(b);
System.out.println(aa.multiply(bb)); }

Multiply Strings 大数相乘 java的更多相关文章

  1. leetcode 43 Multiply Strings 大数相乘

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

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

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

  3. Multiply Strings 字符串相乘

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

  4. 华为上机测试题(大数相乘-java)

    PS:这个不是自己写的,测试OK,供参考. /** * 大数相乘 */ public class BigData { public static void main(String[] args) { ...

  5. Leetcode43. Multiply Strings字符串相乘(大数相乘)

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

  6. 两个大数相乘-Java

    两个字符串表示两个非常大的数,请设计算法计算这两个大数的乘积,结果用字符串表示.例如S1="7832974972840919321747983209327",S2="19 ...

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

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

  8. 43. Multiply Strings 字符串相乘

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

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

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

随机推荐

  1. Binary Tree Level Order Traversal 解答

    Question Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...

  2. (转载)HDU4565

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 这个博客讲的比较好:http://blog.csdn.net/ljd4305/article/d ...

  3. UILocalNotification

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  4. 使用公用表表达式(CTE)

    本文来自:http://blog.csdn.net/songjie521/article/details/3321030 通用表表达式(CTEs)是SQL Server 2005的一项新功能.它们类似 ...

  5. iOS UIScrollView的简单使用

    本文代码下载 http://vdisk.weibo.com/s/BDn59yfnBVMAJ // // ViewController.m // ScrollView_T1119 // // Creat ...

  6. 【二分答案】 【POJ3497】 【Northwestern Europe 2007】 Assemble 组装电脑

    Assemble Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3171   Accepted: 1013 Descript ...

  7. linux进程间通信之共享内存篇

    本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...

  8. Android设置背景

    一.设置图片背景 首先你先将一个的背景图片存入工程中res/drawble(当然drawble-hdpi.drawble-mdpi.drawble-ldpi中一个或者几个文件夹都可)文件夹中.假如我存 ...

  9. swfobject.js 2.2简单使用方法

    swfobject.js 2.2简单使用方法 官方网址介绍http://code.google.com/p/swfobject/wiki/documentation 用法:html部分<div ...

  10. 程序员使用Node的十个技巧

    从问世到现在将近20年,JavaScript一直缺乏其它有吸引力的编程语言,比如Python和Ruby,的很多优点:命令行界面,REPL,包管理器,以及组织良好的开源社区.感谢Node.js和npm, ...