题目如下:

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]:continuereturn cmp(lv1[i],lv2[i])return len(lv1) - len(lv2)letter.sort(cmp = cmpf)return letter + digit

转载于:https://www.cnblogs.com/seyjs/p/9958017.html

【leetcode】937. Reorder Log Files相关推荐

  1. LeetCode:937. Reorder Log Files

    051401 题目 You have an array of logs. Each log is a space delimited string of words. For each log, th ...

  2. Leetcode PHP题解--D54 937. Reorder Log Files

    D54 937. Reorder Log Files 题目链接 937. Reorder Log Files 题目分析 给定一个数组,每一个元素是一条"日志". 每一条日志的第一个 ...

  3. 【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 ...

  4. 【LeetCode】2022 7月 每日一题

    [LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...

  5. 概率p输出1,概率1-p输出0,等概率输出0和1 【LeetCode】470. rand7()构造rand10() 系列变形(新浪、字节面试题)

    目录 1. 等概率输出0和1 1.1 题目描述 1.2 解题思路 & 代码 2. 以 1/N 的概率返回 1~N 之间的数 3. 给定函数rand5() 构造rand7() 或 rand7() ...

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

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

  7. 【LeetCode】﹝数学规律ி﹞第N位数字、可怜的小猪

    [LeetCode]﹝数学规律ி﹞第N位数字.可怜的小猪 文章目录 [LeetCode]﹝数学规律ி﹞第N位数字.可怜的小猪 乐团站位★ 罗马数字转整数★ 整数转罗马数字★★ 第 N 位数字★★ 数字 ...

  8. HOT 100(61~80)【LeetCode】

    HOT 100(61~80)[LeetCode] HHOT 100(61~80) 前言 推荐 207. 课程表[中等] 208. 实现 Trie (前缀树)[中等] 215. 数组中的第K个最大元素[ ...

  9. 【LeetCode】332. 重新安排行程

    [LeetCode]332. 重新安排行程 前言 最近在刷回溯专题的题目,前几种题型(排列.组合.子集.子序列)如果直接套用模板,加一些剪枝,随便过的那种 但是这一题如果带入普通的回溯模板,可能无从下 ...

最新文章

  1. 奔图打印机显示未连接_手机连接奔图打印机,无法打印的解决方法
  2. 扩展用户体验之操作栏ActionBar
  3. 【Linux系统编程】Linux文件操作
  4. 逆向工程核心原理学习笔记(十三):分析abex' crackme #1 的延伸:将参数压入栈
  5. 我的世界javamod怎么装_「装修细节」除了中央空调外的高颜值装空调大法
  6. Butterknife全方位解析
  7. 一个叫花子的故事(十个人看完十个人开悟)
  8. 洞察CRM对企业的真正价值
  9. java 两个list相等_java 怎么比较两个list是否相同
  10. matlab闭式网络潮流计算,闭式网络潮流计算.ppt
  11. Python实战从入门到精通第二十一讲——构建一个模块的层级包
  12. python判断_python判断与或
  13. C语言打印数据的二进制格式-原理解析与编程实现
  14. 计算机声音音乐小星星,迷你世界电路音乐教学 小星星音乐电路
  15. excel2019保存文件为csv utf-8
  16. 学计算机画素描吗,用电脑制作一张素描画
  17. 3.1 YOLO系列理论(YOLOV1、YOLOV2、YOLOV3)
  18. 企业如何搭建呼叫中心?
  19. C-DOCSIS PHS,服务组
  20. 申报高新技术企业认定哪些地方需要注意?

热门文章

  1. 鸿蒙开发-基础组件介绍及chart组件使用
  2. Android中实现照片滑动时左右进出的动画的xml代码
  3. Winform中设置BackgroundWorker在取消时关闭后台进程
  4. Winforn中设置ZedGraph多条Y轴时曲线刻度不均匀问题解决
  5. ansible使用教程(4W字长文,保姆级别教程,建议收藏)
  6. 数据库系统概念总结:第一章 引言
  7. 找不到 tools.jar。请检查 C:\Program Files\Java\jre1.8.0_151 是否包含有效的 JDK 安装
  8. AndroidStudio创建第一个 Flutter 应用程序
  9. 有关数组的运算 (sizeof)(strlen)
  10. 短融网杨夏耘:数据如何驱动金融科技业务升级?