【leetcode】937. Reorder Log Files
题目如下:
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:
0 <= logs.length <= 100
3 <= logs[i].length <= 100
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的更多相关文章
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- 【Leetcode_easy】937. Reorder Log Files
problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 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 ...
- 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 ...
- leecode 937 Reorder Log Files (模拟)
传送门:点我 You have an array of logs. Each log is a space delimited string of words. For each log, the ...
- 【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 ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
随机推荐
- MongoDB笔记【2】——基本概念和基本指令
- 基本概念 数据库(database) 集合(collection) 文档(document) - 在MongoDB中,数据库和集合都不需要手动创建,当我们创建文档时,如果文档所在的集合或数据库不存 ...
- C++ LinearRegression代码实现
这里基本完全参考网络资源完成,有疑问欢迎留言! LinearRegression.h #pragma once #ifndef ML_LINEAEEEGRESSION_H #define ML_LIN ...
- Java Web学习总结(1)Tomcat使用教程
一,简介 Tomcat是一个实现了JAVA EE标准的最小的WEB服务器,是Apache 软件基金会的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.因 ...
- [USACO09DEC] Video Game Troubles
背包DP:有依赖的背包问题 #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...
- python中对列表元素大小排序(冒泡排序法和选择排序法)
前言:排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列.本文主要讲述python中经常用的两种排序算法,选择排序法 ...
- 测开之路二十:比较v1和v2
根据V1和V2的版本号,如果v1>v2,返回1,如果v1<v2,返回-1,除此之外返回0 # 如果v1>v2,返回1,如果v1<v2,返回-1,除此之外返回0v1 = inpu ...
- Java 时间相关
java的时间主要关注这几个类,查看Java API 1.6 java.util.Calendar Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MON ...
- 加载的DAL数据访问层的类型
using System; using System.Collections; using System.Reflection; using CSFrameworkV4_5.Core; using C ...
- linux文本查看与搜索
1. cat-->全文本显示 cat file #全文本显示在终端 cat -n file #显示全文本,并显示行号 cat file1 file2 >file3 #将file1 file ...
- do_mmap解读
1: unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, 2: unsigned long len, unsigned ...