imashutosh51

Largest Number

Oct 8th, 2022 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. def myfun(a,b):
  2.     if a+b<b+a:
  3.         return -1
  4.     elif a+b>b+a:
  5.         return 1
  6.     else:
  7.         return 0
  8.  
  9. class Solution:
  10.     def largestNumber(self, nums: List[int]) -> str:
  11.         nums=[str(i) for i in nums]
  12.         nums.sort(reverse=True, key=cmp_to_key(myfun))
  13.         ans=""
  14.         for i in nums:
  15.             if ans=="" and i=='0': #this is needed to remove the initial zeros
  16.                 continue
  17.             ans+=i
  18.         return ans if ans!="" else '0' #this is needed if input arr=['0','0']
  19.        
Advertisement
Add Comment
Please, Sign In to add comment