当前位置:网站首页>Rearranging log files for leetcode simple question
Rearranging log files for leetcode simple question
2022-04-23 08:14:00 【·Starry Sea】
subject
Give you a log array logs. Each log is a space separated string , The first word is a mixture of letters and numbers identifier .
There are two different types of logs :
Alphabet log : Except identifier , All words consist of lowercase letters
Digital log : Except identifier , All words consist of numbers
Please reorder the logs according to the following rules :
all Alphabet log All in line Digital log Before .
Alphabet log When the content is different , After ignoring the identifier , Sort the contents alphabetically ; When the content is the same , Sort by identifier .
Digital log The original relative order should be retained .
Return the final order of logs .
Example 1:
Input :logs = [“dig1 8 1 5 1”,“let1 art can”,“dig2 3 6”,“let2 own kit dig”,“let3 art zero”]
Output :[“let1 art can”,“let3 art zero”,“let2 own kit dig”,“dig1 8 1 5 1”,“dig2 3 6”]
explain :
The contents of the alphabetic log are different , So the order is “art can”, “art zero”, “own kit dig” .
The digital log retains the original relative order “dig1 8 1 5 1”, “dig2 3 6” .
Example 2:
Input :logs = [“a1 9 2 3 1”,“g1 act car”,“zo4 4 7”,“ab1 off key dog”,“a8 act zoo”]
Output :[“g1 act car”,“a8 act zoo”,“ab1 off key dog”,“a1 9 2 3 1”,“zo4 4 7”]
Tips :
1 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i] in , Between words Single The blank space to separate
Topic data assurance logs[i] Each has an identifier , And there is at least one word after the identifier
source : Power button (LeetCode)
Their thinking
This question needs to rewrite the sorting information according to the conditions , If the first space in each element is followed by a number, it is concluded that this is a digital log , Otherwise, it's the letter log , So we just need to look at the first character after the first space of a single element . because python Self contained sort The default function is stable sorting , Therefore, when arranging the digital log, it is good to directly assign a value of the same size , The rest of the rules can use tuples to separate the first and second order .
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
def sort_rule(x):
index=x.index(' ')
if x[index+1].isdigit():
return 'z'*100,'z'*100
else:
return x[index+1:],x[0:index]
return sorted(logs,key=sort_rule)
版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590459.html
边栏推荐
猜你喜欢
随机推荐
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
搜一下导航完整程序源码
单点登录 SSO
1216_MISRA_C规范学习笔记_控制流的规则要求
Face to face summary 2
dried food! Point based: differentiable Poisson solver
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
Talking about distributed storage from ES, mongodb, redis and rocketmq
LeetCode15. 三数之和
AAAI 2022 recruit speakers!!
Kubernetes in browser and IDE | interactive learning platform killercoda
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
3C裝配中的機械臂運動規劃
NLLLoss+log_SoftMax=CE_Loss
PHP high precision computing
高精度焊接机械臂定位
刨析——浏览器如何工作
华硕笔记本电脑重装系统后不能读取usb,不能上网
Go语学习笔记 - 数组 | 从零开始Go语言
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test