列表

详情


937. 重新排列日志文件

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

有两种不同类型的日志:

请按下述规则将日志重新排序:

返回日志的最终顺序。

 

示例 1:

输入:logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
输出:["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
解释:
字母日志的内容都不同,所以顺序为 "art can", "art zero", "own kit dig" 。
数字日志保留原来的相对顺序 "dig1 8 1 5 1", "dig2 3 6" 。

示例 2:

输入:logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
输出:["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: vector<string> reorderLogFiles(vector<string>& logs) { } };

python3 解法, 执行用时: 36 ms, 内存消耗: 13.5 MB, 提交时间: 2020-11-04 23:09:30

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        log1, log2 = [], []
        for log in logs:
            if log[-1].isalpha():
                log1.append(log)
            else:
                log2.append(log)
        log1.sort(key=lambda x:(x[x.index(' ')+1:], x[:x.index(' ')+1]))
            
        return log1 + log2

上一题