[LeetCode] 937. Reorder Data in Log Files

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

  • Letter-logs: All words (except the identifier) consist of lowercase English letters.
  • Digit-logs: All words (except the identifier) consist of digits.

Reorder these logs so that:

  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering.

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"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

Example 2:

Input: logs = ["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"]

Constraints:

  • 1 <= logs.length <= 100
  • 3 <= logs[i].length <= 100
  • All the tokens of logs[i] are separated by a single space.
  • logs[i] is guaranteed to have an identifier and at least one word after the identifier.

重新排列日志文件。

给你一个日志数组 logs。每条日志都是以空格分隔的字串,其第一个字为字母与数字混合的 标识符 。

有两种不同类型的日志:

字母日志:除标识符之外,所有字均由小写字母组成
数字日志:除标识符之外,所有字均由数字组成
请按下述规则将日志重新排序:

所有 字母日志 都排在 数字日志 之前。
字母日志 在内容不同时,忽略标识符后,按内容字母顺序排序;在内容相同时,按标识符排序。
数字日志 应该保留原来的相对顺序。
返回日志的最终顺序。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是先将字母日志和数字日志分开,然后仅对字母日志排序,数字日志无需排序,最后再合并两种日志。具体步骤如下

  • 通过每个字符串的最后一个字符判断当前日志属于字母还是数字,并放到不同的 list 里,这里我分别定义为 letters 和 digits
  • 对于字母日志 letters 的排序,因为日志的大致格式是 [标识符 + 日志内容],所以这里我们两两比较的时候,先看日志内容是否一样,如果内容一样,则按日志整体的字典序排序;如果日志内容不一样,则仅按日志内容的字典序排序
  • 最后将排好序的 letters 和 digits 两个 list 再写回结果集

时间O(nlogn)

空间O(nlogn) - 默认的快排是需要一定空间的

JavaScript实现

 1 /**
 2  * @param {string[]} logs
 3  * @return {string[]}
 4  */
 5 var reorderLogFiles = function (logs) {
 6     var letters = [], nums = [];
 7 
 8     // Separate digit-logs from letter-logs
 9     logs.forEach(function (log) {
10         if (log.split(" ")[1].charAt(0) >= '0' && log.split(" ")[1].charAt(0) <= '9') {
11             nums.push(log);
12         } else {
13             letters.push(log);
14         }
15     });
16 
17     // Sort letter-logs
18     letters.sort(function (a, b) {
19         var cmp = a.slice(a.indexOf(" ")).localeCompare(b.slice(b.indexOf(" ")));
20         return cmp === 0 ? a.localeCompare(b) : cmp;
21     });
22     return [...letters, ...nums];
23 };

 

Java实现

 1 class Solution {
 2     public String[] reorderLogFiles(String[] logs) {
 3         // corner case
 4         if (logs == null || logs.length == 0) {
 5             return logs;
 6         }
 7 
 8         // normal case
 9         int n = logs.length;
10         List<String> digits = new ArrayList<>();
11         List<String> letters = new ArrayList<>();
12         for (String log : logs) {
13             char last = log.charAt(log.length() - 1);
14             if (last >= '0' && last <= '9') {
15                 digits.add(log);
16             } else {
17                 letters.add(log);
18             }
19         }
20 
21         Collections.sort(letters, new Comparator<>() {
22             @Override
23             public int compare(String s1, String s2) {
24                 int index1 = s1.indexOf(" ");
25                 String letter1 = s1.substring(index1 + 1);
26                 int index2 = s2.indexOf(" ");
27                 String letter2 = s2.substring(index2 + 1);
28                 if (letter1.equals(letter2)) {
29                     return s1.compareTo(s2);
30                 }
31                 return letter1.compareTo(letter2);
32             }
33         });
34 
35         String[] res = new String[n];
36         int i = 0;
37         for (String str : letters) {
38             res[i++] = str;
39         }
40         for (String str : digits) {
41             res[i++] = str;
42         }
43         return res;
44     }
45 }

 

LeetCode 题目总结

posted @ 2020-03-03 02:14  CNoodle  阅读(194)  评论(0)    收藏  举报