题目要求

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 解题报告的更多相关文章

  1. 【LeetCode】937. Reorder Log Files 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...

  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 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  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. sql1032n sql6048n db2start启动不了 db2修改hostname

    今天下午把虚拟机上的linux的hostanme改掉了 结果启动DB2的时候发生了这样的错误 SQL6048N  A communication error occurred during START ...

  2. 初试 Kubernetes 集群中使用 Traefik 反向代理

    初试 Kubernetes 集群中使用 Traefik 反向代理 2017年11月17日 09:47:20 哎_小羊_168 阅读数:12308    版权声明:本文为博主原创文章,未经博主允许不得转 ...

  3. Android Studio下jni应用

    最近在将一个小应用从eclipse开发迁移到android studio,程序中有native代码实现,在eclipse是靠Android.mk这么个mk文件来组织编译的,但到android stud ...

  4. 极速打包【shell版】

    同步发表至 http://avenwu.github.io/2014/12/16/fast_apk_release/ 前言 前阵子无意间看到美团的技术文章,一口气读了几篇java.android相关的 ...

  5. Storm常见模式——流聚合

    转自:http://www.cnblogs.com/panfeng412/archive/2012/06/04/storm-common-patterns-of-stream-join.html 流聚 ...

  6. 【转】搭建Java版WebService

    原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/22/2296052.html Hi,大家好! 今天主要和大家分享,如何搭建一个Web服务,做A ...

  7. java.sql.SQLException: ORA-01578: ORACLE 数据块损坏问题解决办法

    错误信息: java.sql.SQLException: ORA-01578: ORACLE 数据块损坏 (文件号 17, 块号 315703) ORA-01110: 数据文件 17: 'D:\ORA ...

  8. Hibernate HQL的使用

    1.简单查询(查询所有) Session session=HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx=s ...

  9. windows命令行(DOS批处理)添加任务计划

    自动创建每周运行一次的计划任务 创建计划任务可用at,schtasks命令,schtasks提供了很多参数 命令schtasks SCHTASKS /Create [/S system [/U use ...

  10. pythonCharm 破解

    操作步骤和IDEA是一样的 只是jar 包不一样 //激活拷贝-javaagent:E:\PythonCharm\PyCharm Community Edition 2017.3.4\bin\Jetb ...