GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/043_Multiply_Strings.py at master · helenagithub/leetcode Expand file tree/ Copy path043_Multiply_Strings.pyMore file actions 79 lines (76 loc) · 2.24 KB / Copy path043_Multiply_Strings.pyFile metadata and controls79 lines (76 loc) · 2.24 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 class Solution(object): # def multiply(self, num1, num2): # """ # :type num1: str # :type num2: str # :rtype: str # """ # res = '' # curr, pos = 0, 0 # if len(num1) > num2: # num1, num2 = num2, num1 # ls1, ls2 = len(num1), len(num2) # mid = [] # for i in reversed(range(ls1)): # curr = 0 # fact = '' # for j in reversed(range(ls2)): # curr = curr + int(num1[i]) * int(num2[j]) # fact = str(curr % 10) + fact # curr /= 10 # if curr > 0: # fact = str(curr) + fact # if int(fact) == 0: # pass # else: # print fact # mid.append(fact + '0' * pos) # pos += 1 # res = self.add_strings(mid) # return res # # # def add_strings(self, s_list): # if len(s_list) == 0: # return '0' # res = '' # curr, pos = 0, 0 # max_ls = max([len(t) for t in s_list]) # while pos < max_ls: # for s in s_list: # if len(s) <= pos: # continue # curr += int(s[len(s) - pos - 1]) # res = str(curr % 10) + res # curr /= 10 # pos += 1 # if curr > 0: # res = str(curr) + res # return res def multiply(self, num1, num2): if num1 == '0' or num2 == '0': return '0' res = '' ls1, ls2, = len(num1), len(num2) ls = ls1 + ls2 # list stores int arr = [0] * ls for i in reversed(range(ls1)): for j in reversed(range(ls2)): # store the direct results of multiply two ints arr[i + j + 1] += int(num1[i]) * int(num2[j]) for i in reversed(range(1, ls)): # digital plus arr[i - 1] += arr[i] / 10 arr[i] %= 10 pos = 0 # to string if arr[pos] == 0: pos += 1 while pos < ls: res = res + str(arr[pos]) pos += 1 return res if __name__ == '__main__': s = Solution() print s.multiply("98", "9") |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |