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:
- The letter-logs come before all digit-logs.
- The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
- 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是先将字母日志和数字日志分开,然后仅对字母日志排序,数字日志无需排序,最后再合并两种日志。判断是字母还是数字的方法是看每个字符串的第二个单词是否是数字(第十行)。
时间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 Arrays.sort(logs, (log1, log2) -> { 4 // 找到第一个空格,空格之前是id,空格之后是log的主体 5 int index1 = log1.indexOf(' '); 6 String id1 = log1.substring(0, index1); 7 String main1 = log1.substring(index1 + 1); 8 9 int index2 = log2.indexOf(' '); 10 String id2 = log2.substring(0, index2); 11 String main2 = log2.substring(index2 + 1); 12 13 // 判断log主体是letters还是digits 14 boolean isDigit1 = Character.isDigit(main1.charAt(0)); 15 boolean isDigit2 = Character.isDigit(main2.charAt(0)); 16 // 如果两者都是letters,内容字典序大的在前 17 if (!isDigit1 && !isDigit2) { 18 int value = main1.compareTo(main2); 19 // 两者内容相同,id字典序大的在前 20 if (value == 0) { 21 return id1.compareTo(id2); 22 } 23 return value; 24 } 25 // 如果两者都是digits则不排序,两者如果只有一个是digits则digits在前 26 return isDigit1 ? (isDigit2 ? 0 : 1) : -1; 27 }); 28 return logs; 29 } 30 }