File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Given two numbers represented as strings, return multiplication of the numbers
3+ as a string.
4+
5+ Note: The numbers can be arbitrarily large and are non-negative.
6+ """
7+
8+
9+ class Solution (object ):
10+ def multiply (self , num1 , num2 ):
11+ """
12+ :type num1: str
13+ :type num2: str
14+ :rtype: str
15+ """
16+ a = num1 [::- 1 ]
17+ b = num2 [::- 1 ]
18+ n = len (a )
19+ m = len (b )
20+ res = ['0' for i in range (n + m )]
21+ for i in range (n ):
22+ c = 0
23+ for j in range (m ):
24+ tmp = int (a [i ]) * int (b [j ]) + int (res [i + j ]) + c
25+ digit = tmp % 10
26+ res [i + j ] = str (digit )
27+ c = tmp / 10
28+ if c > 0 :
29+ res [m + i ] = str (c )
30+ res = '' .join (res [::- 1 ])
31+ for i , d in enumerate (res ):
32+ if d != '0' :
33+ return res [i :]
34+ else :
35+ return '0'
36+
37+ s = Solution ()
38+
39+ print s .multiply ('2' , '21' )
40+ print s .multiply ('83' , '3' )
You can’t perform that action at this time.
0 commit comments