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: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

Constraints:

  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.

这道题让给日志排序,每条日志是由空格隔开的一些字符串,第一个字符串是标识符,可能由字母和数字组成,后面的是日志的内容,只有两种形式的,要么都是数字的,要么都是字母的。排序的规则是对于内容是字母的日志,按照字母顺序进行排序,假如内容相同,则按照标识符的字母顺序排。而对于内容的是数字的日志,放到最后面,且其顺序相对于原顺序保持不变。博主感觉这道题似曾相识啊,貌似之前在很多 OA 中见过,最后还是被 LeetCode 收入囊中了。其实这道题就是个比较复杂的排序的问题,两种日志需要分开处理,对于数字日志,不需要排序,但要记录其原始顺序。这里就可以用一个数组专门来保存数字日志,这样最后加到结果 res 后面,就可以保持其原来顺序。关键是要对字母型日志进行排序,同时还要把标识符提取出来,这样在遍历日志的时候,先找到第一空格的位置,这样前面的部分就是标识符了,后面的内容就是日志内容了,此时判断紧跟空格位置的字符,假如是数字的话,说明当前日志是数字型的,加入数组 digitLogs 中,并继续循环。如果不是的话,将两部分分开,存入到一个二维数组 data 中。之后要对 data 数组进行排序,并需要重写排序规则,要根据日志内容排序,若日志内容相等,则根据标识符排序。最后把排序好的日志按顺序合并,存入结果 res 中,最后别忘了把数字型日志也加入 res, 参见代码如下:

class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> res, digitLogs;
vector<vector<string>> data;
for (string log : logs) {
auto pos = log.find(" ");
if (log[pos + 1] >= '0' && log[pos + 1] <= '9') {
digitLogs.push_back(log);
continue;
}
data.push_back({log.substr(0, pos), log.substr(pos + 1)});
}
sort(data.begin(), data.end(), [](vector<string>& a, vector<string>& b) {
return a[1] < b[1] || (a[1] == b[1] && a[0] < b[0]);
});
for (auto &a : data) {
res.push_back(a[0] + " " + a[1]);
}
for (string log : digitLogs) res.push_back(log);
return res;
}
};

参考资料:

https://leetcode.com/problems/reorder-data-in-log-files/

https://leetcode.com/problems/reorder-data-in-log-files/discuss/192438/C%2B%2B-O(NlogN)-Time-O(N)-Space

https://leetcode.com/problems/reorder-data-in-log-files/discuss/193656/C%2B%2B-stable_sort-easy-to-understand

https://leetcode.com/problems/reorder-data-in-log-files/discuss/193872/Java-Nothing-Fancy-15-lines-2ms-all-clear.

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序的更多相关文章

  1. /VAR/LOG/各个日志文件分析

     /VAR/LOG/各个日志文件分析 author:headsen  chen    2017-10-24   18:00:24 部分内容取自网上搜索,部分内容为自己整理的,特此声明. 1.   /v ...

  2. Linux没有/var/log/messages日志文件

    1.新安装的CentOS8没有/var/log/messages日志文件: 安装rsyslog: dnf  install   -y  rsyslog 或 yum  install  -y  rsys ...

  3. Android ANR log trace日志文件分析

      版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_25804863/article/ ...

  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. Tomcat的JVM经常挂掉,根据hs_err_pid23224.log这种日志文件,也没能发现具体是什么原因导致的

    ## A fatal error has been detected by the Java Runtime Environment:##  SIGBUS (0x7) at pc=0x00007f1a ...

  6. sql server无log ldf日志文件附件mdf数据库重新生成ldf日志文件

    CREATE DATABASE TestDB    ON    (    FILENAME ='D:\TestDB.mdf' --路径     ) for ATTACH_REBUILD_LOG    

  7. gitlab svlogd runsv 基于Rotated Log的日志统计

    小结: 1. 日志轮询 log roate 日志文件自动转存和重命名 2. rotated log独立于其他模块,可以以静态库或者动态库的形式支持二次开发: 3. [root@d1 ~]# gitla ...

  8. /var/log各种日志

    文章为装载 1)/var/log/secure:记录登录系统存取数据的文件;例如:pop3,ssh,telnet,ftp等都会记录在此. 2)/ar/log/btmp:记录登录这的信息记录,被编码过, ...

  9. logback的使用和logback.xml详解,在Spring项目中使用log打印日志

    logback的使用和logback.xml详解 一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分 ...

随机推荐

  1. 算发帖&mdash;&mdash;俄罗斯方块覆盖问题一共有多少个解

    问题的提出:如下图,用13块俄罗斯方块覆盖8*8的正方形.   那么一共可以有多少个解呢?(若通过旋转.翻转一个解而得到的新解,则两个解视为同一个解)   首先,求解的问题,已经在上一篇帖子里完成 算 ...

  2. Nuxt 项目性能优化调研

    性能优化,这是面试中经常会聊到的话题.我觉得性能优化应该因具体场景而异,因不同项目而异,不同的手段不同的方案并不一定适合所有项目,当然这其中不乏一些普适的方案,比如耳熟能详的文件压缩,文件缓存,CDN ...

  3. 一次js自定义播放器,canvas绘制弹幕的尝试

    不多bb,就直接说实现了什么功能: 1. 视频播放进度调整 2. 视频小窗口实时预览 3. 声音调整 4. 画中画模式 5. 网页全屏 6. 视频全屏 7. canvas绘制弹幕 8. 选中弹幕悬停 ...

  4. 项目页面集成ckeditor富文本编辑器

    步骤一.引入ckeditor.js (注:本实例以ThinkPHP3.2框架为载体,不熟悉ThinkPHP的朋友请自行补习,ckeditor文件代码内容也请去ckeditor官网自行下载) 作为程序员 ...

  5. 初识 jquery.simulate.js 模拟键盘事件

    用jquery 和 jquery.simulate.js 实现模拟键盘事件,点击上下左右div相当于点击键盘的上下左右键 <!DOCTYPE html> <html> < ...

  6. 编写简单i18n库

    i18n是什么?i18n(其来源是英文单词internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称. 前言 第一次接触多语言是用野生java ...

  7. Error : Failed to get convolution algorithm. This is probably because cuDNN failed to initialize

    记录一下: 报错:# Error : Failed to get convolution algorithm. This is probably because cuDNN failed to ini ...

  8. 关于org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.atguigu.crud.dao.DepartmentMapper.insertSelective的错误

    今天我在使用mybatis逆向工程的时候,由于一个疏忽字打错了..结果花了一早上才把错误找全..广大小伙伴们一定要小心啊(能复制粘贴就别手打) 关于org.apache.ibatis.binding. ...

  9. vue自定义指令要点

    vue自定义指令的基础使用这里就不阐述,看官网文档:https://cn.vuejs.org/v2/guide/custom-directive.html 本文用一个实例描述自定义指令的要点,自定义一 ...

  10. 微信小程序结构目录、配置介绍、视图层(数据绑定,运算,列表渲染,条件渲染)

    目录 一.小程序结构目录 1.1 小程序文件结构和传统web对比 1.2 基本的项目目录 二.配置介绍 2.1 配置介绍 2.2 全局配置app.json 2.3 page.json 三.视图层 3. ...