当前位置:网站首页>Sword finger offer 45. arrange the array into the smallest number
Sword finger offer 45. arrange the array into the smallest number
2022-07-24 19:27:00 【nsq1101】
subject
Enter an array of nonnegative integers , Put all the numbers in the array together to form a number , Print the smallest of all the numbers that can be spliced .
Example 1:
Input : [10,2]
Output : "102"
Example 2:
Input : [3,30,34,5,9]
Output : "3033459"
Tips :
0 < nums.length <= 100
explain :
The output can be very large , So you need to return a string instead of an integer
The stitched numbers may have a lead 0, The final result does not need to remove the lead 0
Ideas
- Convert to character array
- The idea of sorting
Program
class Solution:
def minNumber(self, nums: List[int]) -> str:
nums_str = [str(num) for num in nums]
def quick_sort(l,r):
if l >= r: return
i,j = l, r
while i < j :
while i < j and nums_str[j] + nums_str[l] >= nums_str[l] + nums_str[j]:
j = j -1
while i < j and nums_str[i] + nums_str[l] <= nums_str[l] + nums_str[i] :
i = i + 1
nums_str[i],nums_str[j] = nums_str[j],nums_str[i]
nums_str[i],nums_str[l]= nums_str[l],nums_str[i]
quick_sort(l,i -1)
quick_sort(i+1,r)
quick_sort(0,len(nums_str)-1)
return ''.join(nums_str)
边栏推荐
- Arrays
- Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
- Common problems of multithreading and concurrent programming (to be continued)
- OpenGL learning (III) glut two-dimensional image rendering
- Nacos introduction and console service installation
- asp. Net core, C # summary about path
- Original reverse compensation and size end
- MySQL1
- Mysql数据库,去重,连接篇
- [question 39] special question for Niuke in-depth learning
猜你喜欢

Machine learning_ Data processing and model evaluation

day 1

This visual analysis library makes it easy for you to play with data science!

Reading notes of XXL job source code

湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭

Original reverse compensation and size end

Sequences, time series and prediction in tessorflow quizs on coursera (II)

Tcl/tk file operation

Biopharmaceutical safety, power supply and production guarantee

拿捏C指针
随机推荐
Software core data protection solution
Channel state information (CSI) conjugate multiplication denoising method
Solve the problem of disconnection due to long idle time after SSH login
OpenGL learning (IV) glut 3D image rendering
Install SSL Certificate in Litespeed web server
pyhanlp安装教程
JVM method call
Virbox compiler, which supports source code encryption of the whole platform and architecture
Siyuan notes V2.1.2 synchronization problem
LSTM and Gru of RNN_ Attention mechanism
JDBC batch inserts 100000 /1million pieces of data
[Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
mysql排序.按字段值排序
Emergency lighting design of large stadiums and gymnasiums
Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
MySQL index principle and query optimization "suggestions collection"
How to select software dongle
Hangdian multi School Game 1 question 3 backpack (XOR dp+bitset)
Mysql8.0 learning record 20 - trigger
Common methods of string class