Java AC 476ms
public class Solution { public String multiply(String num1, String num2) { if ("".equals(num1.replace("0", "")) || "".equals(num2.replace("0", ""))) { return "0"; } int len1 = num1.length(); int len2 = num2.length(); char arrayA[]; char arrayB[]; arrayA = num1.toCharArray(); arrayB = num2.toCharArray(); int column = len1 + len2; int array[] = new int[column]; int k = column; for (int i = len2 - 1; i >= 0; i--) { k--; int tempCol = k; int tempArr[] = new int[column]; int num = arrayB[i] - '0'; for (int j = len1 - 1; j >= 0; j--) { tempArr[tempCol] += (arrayA[j] - '0') * num; if (tempArr[tempCol] >= 10) { int mod = tempArr[tempCol] / 10; tempArr[tempCol] %= 10; tempArr[tempCol - 1] += mod; } tempCol--; } array = addNum(tempArr, array, column); } StringBuffer sb = new StringBuffer(); int i = 0; if (array[0] == 0) { i = 1; } for (; i < column; i++) { sb.append(array[i]); } return sb.toString(); } public int[] addNum(int tempArr[], int array[], int column) { for (int i = column - 1; i >= 0; i--) { array[i] += tempArr[i]; if (array[i] >= 10) { array[i] -= 10; array[i - 1] += 1; } } return array; } }Python AC 944ms
class Solution: # @param num1, a string # @param num2, a string # @return a string def multiply(self, num1, num2): if int(num1) == 0 or int(num2) == 0: return '0' len1 = len(num1) len2 = len(num2) col = len1 + len2 list = [0 for i in range(col)] list1 = [int(num1[i]) for i in range(len1)] list2 = [int(num2[i]) for i in range(len2)] k = col for i in range(len2-1, -1, -1): k -= 1 tempCol = k tempList = [0 for l in range(col)] for j in range(len1-1, -1, -1): tempList[tempCol] += list1[j] * list2[i] if tempList[tempCol] >= 10 : mod = tempList[tempCol] / 10 tempList[tempCol] %= 10 tempList[tempCol - 1] += mod tempCol -= 1 list = self.addNum(tempList, list, col) if list[0] == 0: list = list[1:] list = [str(e) for e in list] return "".join(list) def addNum(self, tempList, list, col): for i in range(col-1, -1, -1): list[i] += tempList[i] if(list[i] >= 10): list[i] -= 10 list[i-1] += 1 return list