【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 <= 1003 <= logs[i].length <= 100logs[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 ...
随机推荐
- 解决swagger跨项目或跨程序集注释不显示问题
背景 我们在使用Swagger生成.NET Core Web Api 项目接口文档时候,发现接口的入参和出参的注释是看不见的,如下: 但是我想要结果是这样: 原因分析以及方案 为什么没有显示注释呢,注 ...
- CDH6.3.1安装hue 报错
x 一.查看日志server运行日志 /var/log/cloudera-scm-server/cloudera-scm-server.log 2019-12-11 17:28:34,201 INFO ...
- getJSON方式请求服务器
register.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- Vue响应式原理的实现-面试必问
Vue2的数据响应式原理 1.什么是defineProperty? defineProperty是设置对象属性,利用属性里的set和get实现了响应式双向绑定: 语法:Object.definePro ...
- 小程序 js 判断 字符串 为空 null
判断字符串是否为空 1 2 3 4 5 var strings = ''; if (string.length == 0) { alert('不能为空'); } 判断字符串是否为“空”字符即用户输入了 ...
- 公司-IT-Mercari:Mercari 百科
ylbtech-公司-IT-Mercari:Mercari 百科 Mercari是一个日本C2C二手交易平台.拥有针对智能手机的C2C(个人与个人之间的电子商务)二手交易APP,此外还提供针对书籍与C ...
- ceph安装过程
创建群集[2019-03-20 18:35:04,232][ceph_deploy.conf][DEBUG ] found configuration file at: /home/sceph/.ce ...
- xml解析用正则解决没有标签的文本的解析不出异常
如 <q>sasas<w>eqwe</w>ddas</q> package com.people.xmlToSql; import java.io.F ...
- 解决让刷新页面时不提示 "重试或取消”对话框
如果刷新一个已经提交过的页面时,系统总是会提示一个 "重试或取消”的对话框.,如果是一个普通的页面,好象也无所谓,有就有,大不了多点一下.但是当我们是在子窗体中刷新父窗体时,就显得有点多余了 ...
- nginx 虚拟主机+反向代理+负载均衡
nginx是一款免费.开源的http服务器,它是由俄罗斯程序设计师开发的,官方测试,nginx能支撑5万的并发量,主要功能有虚拟主机.反向代理和负载均衡等. nginx配置 # 全局块 ... # e ...