classLargerNumKey1(str):# https://blog.csdn.net/LaoYuanPython/article/details/95042104def__lt__(x, y):return x + y > y + x
classSolution1:deflargestNumber(self, nums):print("input:nums:",nums)
largest_num =''.join(sorted(map(str, nums), key=LargerNumKey1))print("largest_num:", largest_num)return'0'if largest_num[0]=='0'else largest_num
nums =[2343,2343,345,6546,654]
s = Solution1()print(s.largestNumber(nums))print("="*10)classLargerNumKey2(str):# https://blog.csdn.net/LaoYuanPython/article/details/95042104def__lt__(x, y):return x + y < y + x
classSolution2:deflargestNumber(self, nums):print("input:nums:",nums)
largest_num =''.join(sorted(map(str, nums), key=LargerNumKey2))print("largest_num:", largest_num)return'0'if largest_num[0]=='0'else largest_num
nums =[2343,2343,345,6546,654]
s = Solution2()print(s.largestNumber(nums))print("="*10)classLargerNumKey3(str):# https://blog.csdn.net/LaoYuanPython/article/details/95042104def__gt__(x, y):return x + y < y + x
classSolution3:deflargestNumber(self, nums):print("input:nums:",nums)
largest_num =''.join(sorted(map(str, nums), key=LargerNumKey3))print("largest_num:", largest_num)return'0'if largest_num[0]=='0'else largest_num
nums =[2343,2343,345,6546,654]
s = Solution3()print(s.largestNumber(nums))print("="*10)classLargerNumKey4(str):# https://blog.csdn.net/LaoYuanPython/article/details/95042104def__gt__(x, y):return x + y > y + x
classSolution4:deflargestNumber(self, nums):print("input:nums:",nums)
largest_num =''.join(sorted(map(str, nums), key=LargerNumKey4))print("largest_num:", largest_num)return'0'if largest_num[0]=='0'else largest_num
nums =[2343,2343,345,6546,654]
s = Solution4()print(s.largestNumber(nums))print("="*10)print("后面几种为啥都不变了啊")
2. 输出
input:nums:[2343,2343,345,6546,654]
largest_num:654665434523432343654665434523432343==========input:nums:[2343,2343,345,6546,654]
largest_num:234323433456546546234323433456546546==========input:nums:[2343,2343,345,6546,654]
largest_num:234323433456546546234323433456546546==========input:nums:[2343,2343,345,6546,654]
largest_num:234323433456546546234323433456546546==========
后面几种为啥都不变了啊
Process finished with exit code 0