Iam_Sandeep

add two large numbers

Oct 21st, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. class Solution:
  2.     def addStrings(self, num1: str, num2: str) -> str:
  3.         x,y,r=deque(),deque(),deque()
  4.         for i in num1:
  5.             x.append(int(i))
  6.         for i in num2:
  7.             y.append(int(i))
  8.         carry=0
  9.         while x or y or carry:
  10.             a=x.pop() if x else 0
  11.             b=y.pop() if y else 0
  12.             res=a+b+carry
  13.             carry,res=divmod(res,10)
  14.             r.appendleft(str(res))
  15.         return "".join(r)
  16.            
  17.            
  18.        
Add Comment
Please, Sign In to add comment