当前位置:网站首页>【LeetCode】179. 最大数
【LeetCode】179. 最大数
2022-06-23 03:32:00 【LawsonAbs】
1 题目
这题如果是第一次做,还是有点儿难度的,关键是如何想到一个比较的原则? 我们可以想到在冒泡排序中,我们比较的是两个数的大小,如果前者较大,则交换,使得较大的那个数放到后面。不停的操作使得每轮中的最大数放到最后一位,代码如下:
for i in range(len(nums)):
for j in range(len(nums)-i-1): # 表示已经排好的
# 按照大小排序,两两交换
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
但是本题是来找如何拼凑得到最大的一个数,比较的原则不再是两个数孰大孰小了,而是两个数的拼接的结果是孰大孰小?
2 思想
类似冒泡排序,不过比较的原则是是否可以组成一个更大数。
3 代码
class Solution:
def largestNumber(self, nums: List[int]) -> str:
# 将最大的放在最后,然后翻转一下数组
for i in range(len(nums)):
for j in range(len(nums)-i-1): # 表示已经排好的
# 按照大小排序,两两交换
if int(str(nums[j]) + str(nums[j+1])) > int(str(nums[j+1]) + str(nums[j])):
nums[j],nums[j+1] = nums[j+1],nums[j]
nums = list(reversed(nums))
res = ""
for i in nums:
res+=str(i)
# 需要去除前导0
res = res.lstrip("0")
if res == "":
return "0"
return res
边栏推荐
- Drill down into handler, looper, messagequeue
- JS to realize character case switching
- Summary of some precautions and problems in the use of tars framework (mengxinxiang)
- How to install redis version 5.0.8 on the pagoda panel
- Batch generation of Codabar codes using Excel files
- Detailed discussion on modular architecture design of MCU firmware
- Network security memorabilia - Summary of vulnerability exploitation events in 2021
- How to store easydss version 3.0 video files in other free disks?
- What are the advantages and difficulties of introducing AI into ISP Technology
- To implement a task scheduling system, it is enough to read this article
猜你喜欢

mysql 数据恢复 (.ibdata1, bin log)
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

Fetch request details

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

新版kali切换最高账户

One of the touchdesigner uses - Download and install
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]

R tree of search tree

innodb_ruby 视角下 MySQL 记录增删改

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
随机推荐
Svn local computer storage configuration
JS counts the number of times a string appears in another string
Enterprise official website applet building tutorial
Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
How to batch generate ean14 barcode through TXT file
Concept and function of ES6 symbol
Why not use math random() ?!
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
6 values in JS are 'false'
Golang resource embedding scheme
Customization of openfeign
[advanced Android] entrusted by kotlin
Stress testing with locust on rainbow
Brief introduction to arm architecture
ABCD identifier of SAP mm initial cycle count
Regeorg actual attack and defense
Minecraft server technology explanation 𞓜 teach you how to get to the ashes from Xiaobai -- server technology explanation
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
Nezha panel modifies logo, small icon and other information
Flink practice tutorial: advanced 7- basic operation and maintenance