Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to arrange arrays into the smallest number by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:06:56 10月18日 Update

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!

Tags: Number sort array minimum complexity character string scheme result topic small to big small method input output two code content leading Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Docker vpn Apple