Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to arrange arrays into the smallest number by LeetCode

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces LeetCode how to arrange the array into the smallest number, the article is very detailed, has a certain reference value, interested friends must read it!

Topic description

Enter an array of non-negative integers, splice all the numbers in the array into a number, and print the smallest of all the numbers that can be concatenated.

0

< nums.length str: # 使用map将原数组的数字转成字符串 nums = list(map(str, nums)) def quicksort(l, r): # 经典快速排序实现 if l >

= r:

Return

Pivot = nums [l]

I, j = l, r

While i

< j: # 只需要把这里改成自定义的排序方法即可 while i < j and nums[j] + pivot >

= pivot + nums [j]:

J-= 1

Nums [I] = nums [j]

# just change this to a custom sorting method

While I < j and nums [I] + pivot str:

Class compare (str):

Def _ _ lt__ (self, x):

Return self + x < x + self

# use the key of python's built-in sorted, pass in a class that overloads _ _ lt__, and customize the sorting

Return'. Join (sorted (map (str, nums), key=compare))

These are all the contents of the article "how to arrange arrays into the smallest number by LeetCode". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report