当前位置:网站首页>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
边栏推荐
- 访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
- 网赚APP资源下载类网站源码
- PHP generates short links: convert numbers to letters and letters to numbers
- Implementation of new
- BUUCTF [ACTF2020 新生赛]Include1
- 3C裝配中的機械臂運動規劃
- Cloud computing skills competition -- Part 2 of openstack private cloud environment
- C language learning record -- use and analysis of string function (2)
- Kubernetes in browser and IDE | interactive learning platform killercoda
- ApplicationReadyEvent的使用
猜你喜欢

Somme numérique de la chaîne de calcul pour un problème simple de leetcode
![BUUCTF [ACTF2020 新生赛]Include1](/img/47/b8f46037f7e9476b8e01e8d6a7857a.png)
BUUCTF [ACTF2020 新生赛]Include1

惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧

社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播

LeetCode简单题之统计字符串中的元音子字符串

如何在SQL Server中导入excel数据,2019版

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

Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)

LeetCode简单题之三除数

Canvas learning Chapter 1
随机推荐
输入 “ net start mysql ”,出现 “ 发生系统错误 5。 拒绝访问 ” 。问题详解
Guoji Beisheng openstack container cloud environment construction
干货!以点为形:可微分的泊松求解器
谈谈那些基础但不简单的股票数据
[go] common concurrency model [generic version]
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
Compiling principle questions - with answers
NLLLoss+log_SoftMax=CE_Loss
在线YAML转XML工具
php生成短链接:将数字转成字母,将字母转成数字
編譯原理題-帶答案
dmp引擎工作总结(2021,光剑)
室内定位技术对比
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
Why are there 1px problems? How?
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
C outputs a two-dimensional array with the following characteristics.
搜一下导航完整程序源码