Advertisement
jinhuang1102

43. Multiply Strings

Oct 27th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. class Solution:
  2.     def add(self, num1, num2):
  3.         if not num1 or not num2:
  4.             return num1 or num2
  5.        
  6.         if num1 == '0':
  7.             return num2
  8.        
  9.         if num2 == '0':
  10.             return num1
  11.        
  12.         long = num1 if len(num1) >= len(num2) else num2
  13.         short = num1 if len(num1) < len(num2) else num2
  14.        
  15.         diff = len(long) - len(short)
  16.        
  17.         long1 = long[:diff]
  18.         long2 = long[diff:]
  19.        
  20.         res = ''
  21.        
  22.         carry = 0
  23.         i = len(short) - 1
  24.         while i >= 0:
  25.             num1 = ord(short[i]) - ord('0')
  26.             num2 = ord(long2[i]) - ord('0')
  27.             temp = num1 + num2 + carry
  28.             d = chr(temp % 10 + ord('0'))
  29.             carry = temp // 10
  30.            
  31.             res = d + res
  32.             i = i - 1
  33.            
  34.         i = len(long1) - 1
  35.         while i >= 0:
  36.             num = ord(long1[i]) - ord('0')
  37.             temp = num + carry
  38.             d = chr(temp % 10 + ord('0'))
  39.             carry = temp // 10
  40.            
  41.             res = d + res
  42.             i = i - 1
  43.            
  44.         if carry:
  45.             d = chr(carry + ord('0'))
  46.             res = d + res
  47.            
  48.         return res
  49.    
  50.    
  51.     def multiply(self, num1, num2):
  52.         """
  53.        :type num1: str
  54.        :type num2: str
  55.        :rtype: str
  56.        """
  57.         if num1 is '0' or num2 is '0':
  58.             return '0'
  59.        
  60.         if not num1 or not num2:
  61.             return '0'
  62.        
  63.         long = num1 if len(num1) >= len(num2) else num2
  64.         short = num1 if len(num1) < len(num2) else num2
  65.        
  66.         res = ''
  67.        
  68.         zero = 0
  69.        
  70.         i = len(short) - 1
  71.         while i >= 0:
  72.             carry = 0
  73.            
  74.             num1 = ord(short[i]) - ord('0')
  75.            
  76.             res_t = '0' * zero
  77.  
  78.             j = len(long) - 1
  79.             while j >= 0:
  80.                 num2 = ord(long[j]) - ord('0')
  81.                 temp = num1 * num2 + carry
  82.                 carry = temp // 10
  83.                 p = chr(temp % 10 + ord('0'))
  84.                 res_t = p + res_t
  85.                
  86.                 j = j - 1
  87.                
  88.             if carry:
  89.                 p = chr(carry + ord('0'))
  90.                 res_t = p + res_t
  91.  
  92.             res = self.add(res, res_t)
  93.            
  94.             zero = zero + 1
  95.            
  96.             i = i - 1
  97.            
  98.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement