LeetCode 937 Reorder Log Files 解题报告
题目要求
You have an array of logs. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
- Each word after the identifier will consist only of lowercase letters, or;
- Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
Return the final order of the logs.
题目分析及思路
给定一组字符串,每个字符串中的内容由空格隔开,且每个字符串开头是一个由数字和字母组成的标识符。之后要么全是数字(digit-logs),要么全是小写单词(letter-logs)。且保证标识符后一定会有内容。最后要求返回的结果是:letter-logs要在digit-logs前面,其中letter-logs要按字母表顺序排列(忽略标识符),digit-logs按原始顺序排列。可以遍历该数组,将标识符和后面的内容分开,将digit-logs按顺序放在单独的数组中,再对letter-logs进行排序。最后将两个列表组合。
python代码
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
final_logs = []
digit_logs = []
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for log in logs:
idx = log.index(' ')
if log[idx+1] in digits:
digit_logs.append(log)
else:
final_logs.append(log)
final_logs.sort(key = lambda l:l[l.index(' ')+1:])
final_logs.extend(digit_logs)
return final_logs
LeetCode 937 Reorder Log Files 解题报告的更多相关文章
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- 【Leetcode_easy】937. Reorder Log Files
problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...
- 【leetcode】937. Reorder Log Files
题目如下: You have an array of logs. Each log is a space delimited string of words. For each log, the f ...
- 【LeetCode】Reorder Log Files(重新排列日志文件)
这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...
- 937. Reorder Log Files
You have an array of logs. Each log is a space delimited string of words. For each log, the first w ...
- leecode 937 Reorder Log Files (模拟)
传送门:点我 You have an array of logs. Each log is a space delimited string of words. For each log, the ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- 按enter键触发登录事件
$(document).keydown(function(event){ if(event.keyCode==13){ $(".submit").click(); } });
- JS中的Map和Set实现映射对象
使用iterable内置的forEach方法 var a = ['A', 'B', 'C']; a.forEach(function (element, index, array) { // elem ...
- MAC /usr/local 文件夹权限问题
修改文件夹权限 sudo chown -R $(whoami) /usr/local/ 如果失败提示Operation not permitted 或其他权限不足,则需要关闭Rootless Root ...
- TestNg学习
参考:https://www.yiibai.com/testng/junit-vs-testng-comparison.html#article-start 1.JUnit缺点: 最初的设计,使用于单 ...
- 图解CentOS系统启动流程
当我们按下开机键后,系统背后的秘密我们是否了解呢?这里,我带大家探索一下linux系统开机背后的秘密. 1.加电自检 主板在接通电源后,系统首先由POST程序来对内部各个设备进行检查,自检中如 ...
- Spark学习笔记——在远程机器中运行WordCount
1.通过realy机器登录relay-shell ssh XXX@XXX 2.登录了跳板机之后,连接可以用的机器 XXXX.bj 3.在本地的idea生成好程序的jar包(word-count_2.1 ...
- Java并发之volatile二
使用volatilekeyword的场景 Volatile 变量具有 synchronized 的可见性特性.可是不具备原子特性.这就是说线程可以自己主动发现 volatile 变量的最新值.Vola ...
- MTK 自定义系统服务
添加系统服务需要添加aidl,service,manager文件,需要修改SystemServer,Context,ContextImpl 下面举例进行说明,主要添加一个服务,用于获取系统时间和版本号 ...
- ElasticSearch入门 第二篇:集群配置
这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- ThinkPHP框架 祖辈分的理解 【儿子 FenyeController】继承了【父亲 FuController】继承了【祖辈 Controller】的
注:系统自带的Controller方法代表的是祖辈 FuController控制器是自定义的,代表父亲... FenyeController控制器就代表着儿子 [儿子 FenyeController] ...