您的位置:首页 > 编程语言 > Java开发

leetCode练习(43)

2016-09-27 11:02 274 查看
题目:Multiply Strings

难度:dedium

问题描述:

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

Note:

The numbers can be arbitrarily large and are non-negative.
Converting the input string to integer is NOT allowed.
You should NOT use internal library such as BigInteger.

解题思路:
用数组模拟乘法的计算过程。面对nums1*nums2,先计算所有的temp【i】=nums1*nums2【i】,然后将每个temp【i】左边填上合适数量的0之后再相加即可。

返回时注意开头的0要舍去。

具体代码如下:

public class Solution {
public String multiply(String num1, String num2) {
if(num1.equals("0")||num2.equals("0")) return "0";
char[] str1=new char[num1.length()];
char[] str2=new char[num2.length()];
int[][] temp=new int[num2.length()][num1.length()+1];
int i;
String rs="";
for(i=0;i=0;i--){
for(int j=s[i].length-1;j>=0;j--){
temp[i][(len--)-(s.length-i)]=s[i][j];
}
len=temp[0].length;
}
for(int i=temp[0].length-1;i>=0;i--){
sum=0;
for(int j=0;j=0;i--){
res[i+1]=(str[i]*m+jinwei)%10;
jinwei=(str[i]*m+jinwei)/10;
}
res[0]=jinwei;
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode 算法