leetCode練習(43)
阿新 • • 發佈:2019-02-11
題目: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; } }