Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: 1. The length of both num1 and num2 is < 110. 2. Both num1 and num2 contains only digits 0-9. 3. Both num1 and num2 does not contain any leading zero. 4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
將兩個String形式的數字相乘的結果用String的形式返回。不許使用Int(java)之外的形式來記錄數字。java
經過隊列存儲每一輪計算的結果。進行下一輪計算的時候,將上一輪的值deque出來加到當前的值上。git
public String multiply(String num1, String num2) { if(num1.equals("0") || num2.equals("0")){ return "0"; } StringBuilder result = new StringBuilder(); //使用鏈表的方式實現隊列 LinkedList<Integer> queue = new LinkedList<Integer>(); int count = 0; //將隊尾的0添加到結果值中,並消去結尾的結果值 if(num1.endsWith("0")){ for(int i = num1.length() - 1 ; i>=0 ; i--){ if(num1.charAt(i) == '0'){ count++; result.append("0"); }else{ break; } } num1 = num1.substring(0, num1.length()-count); } count = 0; if(num2.endsWith("0")){ for(int i = num2.length() - 1 ; i>=0 ; i--){ if(num2.charAt(i) == '0'){ count++; result.append("0"); }else{ break; } } num2 = num2.substring(0, num2.length()-count); } for(int i = num1.length()-1 ; i>=0 ; i--){ //乘數的值,若是乘數爲0,則直接將上一輪的值添加到結果值 int number1 = num1.charAt(i) - '0'; if(number1 == 0){ result.append(queue.removeFirst()); //補進位0 queue.add(0); continue; } int carry = 0; for(int j = num2.length()-1 ; j>=0 ; j--){ //被乘數的值 int number2 = num2.charAt(j) - '0'; //第一輪無需考慮上一輪的進位 int currentVal = number1 * number2 + carry + (i==num1.length()-1?0:queue.removeFirst()); //若是是這一輪的末尾,直接將值添加到結果值中 if(j== num2.length()-1){ result.append(currentVal % 10); }else{ queue.add(currentVal % 10); } carry = currentVal/10; } queue.add(carry); carry = 0; } while(!queue.isEmpty() && queue.getLast() == 0){ queue.removeLast(); } //將隊列中的非零開頭的進位添加到結果中 while(!queue.isEmpty()){ result.append(queue.removeFirst()); } return result.reverse().toString(); }
根據乘法計算的規則,咱們能夠判斷兩個值計算後應該填到哪個位置上。假設num1[i]*num2[j],則將結果的十位和個位分別放在數組下標爲i+j和i+j+1的位置上。記得計算的時候要加上上一輪的進位。面試
public String multiply(String num1, String num2) { int m = num1.length(), n = num2.length(); int[] pos = new int[m + n]; for(int i = m - 1; i >= 0; i--) { for(int j = n - 1; j >= 0; j--) { int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0'); int p1 = i + j, p2 = i + j + 1; int sum = mul + pos[p2]; pos[p1] += sum / 10; pos[p2] = (sum) % 10; } } StringBuilder sb = new StringBuilder(); for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p); return sb.length() == 0 ? "0" : sb.toString(); }
這裏將每一位的計算結果都先存儲到當前位置上,無論是否進位。存儲的位置等同於上一思路。而後再經過一輪遍歷將進位處理一下。數組
public String multiply(String num1, String num2) { if(num1.isEmpty() || num2.isEmpty()) return "0"; int m = num1.length(), n = num2.length(); int[] ret = new int[m+n]; for(int i = m-1; i >= 0; i--) { int n1 = num1.charAt(i)-'0'; for(int j = n-1; j>=0; j--) { int n2 = num2.charAt(j)-'0'; int mul = n1*n2; ret[i+j+1] += mul; } } int carryOver = 0; for(int i = ret.length-1; i>=0; i--) { ret[i]+=carryOver; carryOver = ret[i]/10; ret[i]%=10; } StringBuilder sb = new StringBuilder(); for(int x : ret) { if(x == 0 && sb.length()==0) continue; sb.append(x); } return sb.length()==0?"0":sb.toString(); }
想要了解更多開發技術,面試教程以及互聯網公司內推,歡迎關注個人微信公衆號!將會不按期的發放福利哦~微信