当前位置:网站首页>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
边栏推荐
- 一个没啥L用,但可以装X的IDEA插件
- Find the largest of 3 strings (no more than 20 characters per string).
- C outputs a two-dimensional array with the following characteristics.
- 有意思的js 代码
- Cloud computing skills competition -- Part 2 of openstack private cloud environment
- BUUCTF [ACTF2020 新生赛]Include1
- LeetCode简单题之统计字符串中的元音子字符串
- An article understands variable lifting
- Ubuntu安装Mysql并查询平均成绩
- [Effective Go 中文翻译] 第一篇
猜你喜欢

【无标题】

为什么会存在1px问题?怎么解决?

Go语学习笔记 - 语言接口 | 从零开始Go语言

1216_MISRA_C规范学习笔记_控制流的规则要求
![BUUCTF [极客大挑战 2019]EasySQL1](/img/ad/afca09bc1da003393319af700e90e3.png)
BUUCTF [极客大挑战 2019]EasySQL1

2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局

【Appium】测试时遇到手机内嵌H5页面的切换问题

NLLLoss+log_SoftMax=CE_Loss

Smart business card applet business card details page function implementation key code

How to import Excel data in SQL server, 2019 Edition
随机推荐
Implementation principle of instanceof
巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
渗透测试面试合集---HVV---
Guoji Beisheng openstack container cloud environment construction
LeetCode15. 三数之和
Face to face summary 2
AAAI 2022 recruit speakers!!
【无标题】
Solidity IDE Remix中文版使用手册
sql 使用过的查询语句
简述CPU
Find the largest of 3 strings (no more than 20 characters per string).
Planification du mouvement du manipulateur dans l'assemblage 3c
以下程序实现从字符串str中删除第i个字符开始的连续n个字
使用 Ingress 实现金丝雀发布
[极客大挑战 2019]Havefun1
Upload labs range practice
岛屿的个数
Anti shake and throttling