leetCode练习(43)

本文介绍了一种不使用内置大数处理库的方法来实现两个大整数的字符串形式相乘的问题。通过模拟乘法过程,逐位进行计算并处理进位,最终得到相乘结果的字符串表示。

题目: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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值