题目要求

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. 【Python】将python3.6软件的py文件打包成exe程序

    下载pyinstaller pyinstaller 改变图标 pyinstaller -F --icon=my.ico xxx.py 采用命令行操作的办法 在cmd命令行中,输入代码: 首先,前往Py ...

  2. LRN和Batch Norm

    LRN LRN全称为Local Response Normalization,局部相应归一化层. message LRNParameter { optional uint32 local_size = ...

  3. 教你一招:修复win7 系统自带的截图工具损坏

    这个问题经常见,原因是注册表没有导入. 修复很简单. 打开资源管理器,在C盘中搜索到 tpcps.dll ,在其中选一个右击,选择注册dll,然后截图工具就被修复了. 有时候便签也会出现类似问题,方法 ...

  4. linux下yum安装最新稳定版nginx

    ## 摘抄nginx官网文档 URL:http://nginx.org/en/linux_packages.html#stable To set up the yum repository for R ...

  5. 修改torndb库为依赖pymysql,使其适应python3,一个更简单的操作数据库的类。

    1.python的MySQLdb和pymysql是两个基本数据库操作包,MySQLdb安装很麻烦,要有c++相关环境,python3也安装不了. python3一般安装pymysql,此包与MySQL ...

  6. 给hmailserver添加DKIM签名

    上一篇说了hmailserver如何设置反垃圾邮件功能,现在来说说如何让自己的hmailserver发出去的邮件不要被别人反垃圾了.在hmailserver的反垃圾邮件功能中有提到给垃圾评分标准,其中 ...

  7. k8s(1)-使用kubeadm安装Kubernetes

    安装前准备 1. 一台或多台主机,这里准备三台机器 角色 IP Hostname 配置(最低) 操作系统版本 主节点 192.168.0.10 master 2核2G CentOS7.6.1810 工 ...

  8. spark未授权RCE漏洞学习

    Spark简介 spark是一个实现快速通用的集群计算平台.它是由加州大学伯克利分校AMP实验室 开发的通用内存并行计算框架,用来构建大型的.低延迟的数据分析应用程序.它扩展了广泛使用的MapRedu ...

  9. mui+回复弹出软键盘

    最近再做一个APP的时候,有一个评论回复的功能,在做APP的时候,直接用手指触发focus事件,来唤醒软键盘的弹出没有问题, 但是现在的功能需要对点击回复进行弹出软键盘来操作,参考过很多都有问题,后来 ...

  10. node.js 简单的获取命令参数

    class Argvs { constructor() { this.argvsAll = this.argvsAll(); } argvsAll() { return process.argv.sl ...