当前位置:网站首页>【LeetCode】179. Maximum number
【LeetCode】179. Maximum number
2022-06-23 03:38:00 【LawsonAbs】
1 subject
If this is the first time to do , It's still a little difficult , The key is how to think of a comparison principle ? We can think of bubble sorting , We compare the size of two numbers , If the former is larger , The exchange , Make the larger number come back . The continuous operation makes the maximum number in each round to the last place , The code is as follows :
for i in range(len(nums)):
for j in range(len(nums)-i-1): # Indicates that it has been arranged
# Sort by size , In exchange
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
But this question is to find out how to piece together to get the largest number , The principle of comparison is no longer the greater of the two numbers , But the result of the splicing of two numbers is which is bigger or smaller ?
2 thought
Sort like bubbling , However, the principle of comparison is whether a larger number can be formed .
3 Code
class Solution:
def largestNumber(self, nums: List[int]) -> str:
# Put the biggest last , Then flip the array
for i in range(len(nums)):
for j in range(len(nums)-i-1): # Indicates that it has been arranged
# Sort by size , In exchange
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)
# Need to remove leading 0
res = res.lstrip("0")
if res == "":
return "0"
return res
边栏推荐
- Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
- SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
- How to print array contents
- TDesign update weekly report (the second week of January 2022)
- How to print multiple barcode labels on one sheet of paper
- Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
- [metauniverse 7ai rope skipping] how is this app light application realized? What are the application scenarios?
- [burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
- Initialize MySQL Gorm through yaml file
- Bi skills - authority control
猜你喜欢

直接插入排序

线上MySQL的自增id用尽怎么办?

Detailed discussion on modular architecture design of MCU firmware

Talk about memory model and memory order

mysql 数据恢复 (.ibdata1, bin log)

1058 multiple choice questions (20 points)
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]

R tree of search tree

冒泡排序法
随机推荐
How to implement collection sorting?
CentOS install redis
Quickly understand the development status of secondary nodes of industrial Internet identity analysis
Minecraft server technology explanation 𞓜 teach you how to get to the ashes from Xiaobai -- server technology explanation
Static code block, code block, constructor execution order
【LeetCode】23. 合并K个升序链表
Get method of fetch request and data of formdata type submitted by post
Stress testing with locust on rainbow
C. Differential Sorting
Not just offline caching- On how to make good use of serviceworker
聊聊内存模型和内存序
Is LinkedList a one-way linked list or a two-way linked list?
Easysearcher cannot find the IP address of easynvr hardware. How to solve it?
[advanced Android] entrusted by kotlin
【二叉树】993. Cousins in Binary Tree
【LeetCode】179. 最大数
Goframe framework: log configuration management
Svn local computer storage configuration
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
线上MySQL的自增id用尽怎么办?