题目要求

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. ThinkPad T420 Fn+F5

    关于F5,可做如下设置:     1)官网win7系统下载SIhotkey[8jvu39ww].exe:最新版本的我没测试,应该也可以用.     2)双击安装,并按程序安装,直到要你选择安装on s ...

  2. Java反射,参数为数组

    使用反射调用非公开的方法有时能解决许多问题,如果方法的参数是数组时类型该怎么传递呢?这里找到了一种方法记录一下 实例 比如: class A{ private void sayHello(String ...

  3. modelsim 中如何加载多个对比波形文件

    首先选中波形,然后点击file----datasets. 选中sim文件,然后点击save as. 对所保存的文件命名.完成之后点击done即可. 在完成上述的加载波形之后,会有多个.wlf文件,如上 ...

  4. 手动升级11.2.0.1的rac数据库到11.2.0.4

    ① 关闭两个节点上的数据库 crsctl stop resource ora.ORA11G.db ② 命令行单节点启动数据库, 注意这里的SQLPLUS 一定是升级后的软件地址 sqlplus / a ...

  5. Tensorflow学习笔记——安装和运行

    参考的资料: 1.官网 2.tensorflow官方文档中文版 安装的是Python 2.7; CPU support (no GPU support),版本是1.1 pip install tens ...

  6. 找出相似的图片--C#

    请先参考我写到java这章 原理讲解 参考Neal Krawetz博士的这篇文章, 实现这种功能的关键技术叫做"感知哈希算法"(Perceptual Hash Algorithm) ...

  7. highCharts图表入门简介

    一.Highcharts简介 Highcharts:功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯js图表库 Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在W ...

  8. ViewPager PagerAdapter not updating the View

    There are several ways to achieve this. The first option is easier, but bit more inefficient. Overri ...

  9. QT 中Widgets-Scene3d例子学习

    QT中自带的例子widgets-scene3d实现在基于Widget的应用程序中使用qml 3d场景的功能,我在此基础上,将basicshapes-cpp的例子加以嵌入: 相关代码如下:  C++ C ...

  10. 【laravel5.6】 laravel中间件内生成参数并且传递到控制器的2种方法

    中间件方法: /** * 自定义中间件: * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return ...