题目如下:

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.

解题思路:题目实在太简单了,我的方法是创建两个数组letter和digit,接下来遍历logs,如果logs[i]的最后一个字符是数字,存入digit;否则,存入letter。遍历完成后,对letter进行排序,最后返回letter + digit。

随便说说:最近真的是太忙了,基本没有时间做题。

代码如下:

class Solution(object):
def reorderLogFiles(self, logs):
"""
:type logs: List[str]
:rtype: List[str]
"""
letter = []
digit = []
for i in logs:
if i[-1].isdigit():
digit.append(i)
else:
letter.append(i)
def cmpf(v1,v2):
lv1 = v1.split(' ')
lv2 = v2.split(' ')
for i in range(1,min(len(lv1),len(lv2))):
if lv1[i] == lv2[i]:
continue
return cmp(lv1[i],lv2[i])
return len(lv1) - len(lv2) letter.sort(cmp = cmpf)
return letter + digit

【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】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

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

  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】143. Reorder List

    Question: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...

  8. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

随机推荐

  1. nuget cli 打包发包

    微软官网打包说明:https://docs.microsoft.com/zh-cn/nuget/quickstart/create-and-publish-a-package-using-visual ...

  2. Struts2基础-3 -继承ActionSupport接口创建Action控制器+javaBean接收请求参数+ 默认Action配置处理请求错误 + 使用ActionContext访问ServletAPI

    1.目录结构及导入的jar包 2.web.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <web ...

  3. flutter网格布局之GridView组件

    前面总结了使用ListView来实现列表,但是,有的时候,数据量很大,需要使用矩阵方式排列才能更清晰的展示数据,在flutter中,可以使用网格列表组件GridView来实现这个布局. GridVie ...

  4. JAVA(JDK,JRE)更改目录安装及环境变量配置

    重温一下 JAVA(JDK,JRE)更改目录安装及环境变量配置 https://jingyan.baidu.com/article/e2284b2b5b7ae5e2e7118d11.html 备注:随 ...

  5. 防止NSTimer和调用对象之间的循环引用

    防止NSTimer和调用对象之间的循环引用 @interface NSTimer (EOCBlocksSupport) + (NSTimer *)eoc_scheduledTimerWithTimeI ...

  6. Sublime Text 3 快捷键总结(Mac)

    Command + Shift + L 光标同时定位多行 Command + Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行.

  7. Thrift报错:Error: Thrift compiler: Failed to translate files. Error: Cannot run program thrift error=2

    文章目录 报错: 原因: 解决: 报错: Error: Thrift compiler: Failed to translate files. Error: Cannot run program th ...

  8. upc组队赛15 Made In Heaven【第K短路 A*】

    Made In Heaven 题目描述 One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with ...

  9. python 使用[[v]*n]*m遇到的问题

    需求:想通过python生成m行n列的矩阵 方式1:(有问题) data = [[0]*3]*4 #4行3列 data 输出 [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, ...

  10. git使用记录八:不同提交的指定文件的差异

    不同提交的指定文件的差异 git diff commit-id1 commit-id2 path-to-filename