【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/ 题目地 ...
随机推荐
- 基于MS SQL Server的数据库学习安排
序号 分类 学习内容 目标/要求 方式 学时 考核 参考资料 1 基础知识 数据库理论 理解数据库基本理论 面授 1 能阐述元素.数据记录.数据表.数据库的基本概念T-SQL语法要求 https:// ...
- 🚀 RabbitMQ课程发布-KuangStudy
RabbitMQ课程上线(44集) 视频教程地址:https://www.kuangstudy.com/course/detail/1323452886432944129 专栏地址:https://w ...
- Spring Cloud中五花八门的分布式组件我到底该怎么学
分布式架构的演进 在软件行业,一个应用服务随着功能越来越复杂,用户量越来越大,尤其是互联网行业流量爆发式的增长,导致我们需要不断的重构应用的结构来支撑庞大的用户量,最终从一个简单的系统主键演变成了一个 ...
- 取gridview中textbox的值【C#】
<asp:GridView ID="gridView" runat="server" OnRowCommand="gridView_RowCom ...
- 文件和目录之间建立链接 (ln)
- postgresql安装部署
一.下载安装: 1.下载: 官网下载地址:https://www.postgresql.org/download/linux/redhat/ 也可以用这个:https://www.enterprise ...
- C++11的auto自动推导类型
auto是C++11的类型推导关键字,很强大 例程看一下它的用法 #include<vector> #include<algorithm> #include<functi ...
- Oracle——概要文件profile
profile文件详解 一.目的 Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对 ...
- Android 高级UI组件(三)
一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...
- java设计模式—Decorator装饰者模式
一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色 抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...