作者: 负雪明烛
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:

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. 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)的更多相关文章

  1. 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 ...

  2. 【Leetcode_easy】937. Reorder Log Files

    problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...

  3. 【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 ...

  4. 【LeetCode】Reorder Log Files(重新排列日志文件)

    这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...

  5. 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 ...

  6. leecode 937 Reorder Log Files (模拟)

    传送门:点我 You have an array of logs.  Each log is a space delimited string of words. For each log, the ...

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. R绘图布局包 customLayout

    今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...

  2. idea中如何找到重写

    Ctrl+O 为了避免写错重写类和快速重写.

  3. error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64

    今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...

  4. Oracle-常用表的查询、增加列、删除列、修改列值功能【增删改查】

    #查看表 select * from `竟企区域数据分析` #在表第一列新增名为"年月"的列alter table `竟企区域数据分析` add column 年月 varchar ...

  5. college-ruled notebook

    TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...

  6. ASP.NET Core中使用固定窗口限流

    算法原理 固定窗口算法又称计数器算法,是一种简单的限流算法.在单位时间内设定一个阈值和一个计数值,每收到一个请求则计数值加一,如果计数值超过阈值则触发限流,如果达不到则请求正常处理,进入下一个单位时间 ...

  7. Android项目的settings.gradle和build.gradle

    gradle构建的项目中的build.gradle和settings.gradle文件 build.gradle 浅析(一) 史上最全的Android build.gradle配置教程 Android ...

  8. 【编程思想】【设计模式】【其他模式】hsm

    Python版 https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py """ Impl ...

  9. spring Profile 为不同环境提供不同的配置支持

    说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ...

  10. jQuery中的html()、text()和val()的用法

    1.html() 获得的是第一个符合要求的标签中的所有内容,例如: var content = $("li").html(); <li>111<p>999& ...