【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/max-points-on-a-line/description/
题目描述
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.
Example 1:
Input: ["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"]
Note:
0 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i]
is guaranteed to have an identifier, and a word after the identifier.
题目大意
Log的格式是第一个单词是Log的索引,后面的都是Log的内容。有两种Log,一种内容是纯数字的,一种内容是纯英文字符的。现在要求,把所有的英文Log放到数字Log前面。而且如果是纯英文的字符Log,需要按照内容对Log进行排序,当内容相同的时候按照索引排序;如果是数字Log,保持原来的顺序。
解题方法
分割和排序
周赛第一题,看起来题目很长,但是只要是字符串处理题,对于python都很简单。首先需要进行分割成索引和内容,然后对内容的第一个单词进行判断,如果是英文字符串,那么把内容和索引构成tuple放到letters的列表里;如果是数字字符串,那么直接把当前的这个log放到nums列表里。
然后我们需要对letters进行排序,因为tuple里首先是内容,然后是索引,所以会先对内容进行排序,然后再对索引进行排序。
把letters排序的结果重置成正常的状态和nums拼接在一起,返回即可。
时间复杂度是O(NlogN),空间复杂度是O(N)。
class Solution(object):
def reorderLogFiles(self, logs):
"""
:type logs: List[str]
:rtype: List[str]
"""
letters = []
nums = []
for log in logs:
logsplit = log.split(" ")
if logsplit[1].isalpha():
letters.append((" ".join(logsplit[1:]), logsplit[0]))
else:
nums.append(log)
letters.sort()
return [letter[1] + " " + letter[0] for letter in letters] + nums
日期
2018 年 11 月 11 日 —— 剁手节快乐
【LeetCode】937. Reorder Log Files 解题报告(Python)的更多相关文章
- LeetCode 937 Reorder Log Files 解题报告
题目要求 You have an array of logs. Each log is a space delimited string of words. For each log, the fi ...
- 【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】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【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/ 题目地 ...
随机推荐
- miRNA分析--数据过滤(一)
miRNA 数据过滤我使用cutadapt 1 cutadapt -a AGATCGGAAGAGCACACGTCT -m 15 -q 20 --discard-untrimmed -o outname ...
- PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组
PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 <?php /** * 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 * @param [type] $st ...
- NAT 工作原理
网络地址转换,就是替换IP报文头部的地址信息.NAT通常部署在一个组织的网络出口位置,通过将内部网络IP地址替换为出口的IP地址提供公网可达性和上层协议的连接能力 规定了三个保留地址段落:10.0.0 ...
- error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
- jquery chosen onchange 值改变时触发方法
jquery chosen onchange 值改变时触发方法如下:$(".chzn-select").chosen().on("change", functi ...
- 大型前端项目 DevOps 沉思录 —— CI 篇
摘要 DevOps 一词源于 Development 和 Operations 的组合,即将软件交付过程中开发与测试运维的环节通过工具链打通,并通过自动化的测试与监控,减少团队的时间损耗,更加高效稳定 ...
- A Child's History of England.24
Besides all these troubles, William the Conqueror was troubled by quarrels among his sons. He had th ...
- Shell学习(六)——条件判断总结
Shell学习(六)--条件判断总结 [1]https://www.cnblogs.com/zhw-626/p/8528001.html [2]https://www.cnblogs.com/yizh ...
- Android EditText软键盘显示隐藏以及“监听”
一.写此文章的起因 本人在做类似于微信.易信等这样的聊天软件时,遇到了一个问题.聊天界面最下面一般类似于如图1这样(这里只是显示了最下面部分,可以参考微信等),有输入文字的EditText和表情按钮等 ...
- Jenkins 关闭和重启的实现方式
关闭jenkins 只需要在访问jenkins服务器的网址url地址后加上exit.例如我jenkins的地址http://localhost:8080/ , 那么我只需要在浏览器地址栏上敲下 htt ...