[leetcode] 937. Reorder Data in Log Files

Description

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.

分析

题目的意思是:给log文件进行排序,对于数字类型的保持原来的顺序,对于字母类型的要按照字符的顺序。python对字符串的排序还是支持不错的,首先对于字母的log,用0标识,数字的用1标识。这样排序的时候字母类型的就排在前面去了,数字排在后面,由于数字类型的log只需要保持原来的顺序,因此就不加标识了;对于字母类型的log,把他按照第一个空格分割成ID,word两部分,先按照word排序,最后按照id排序就是最终的答案了哈。

代码

class Solution:
    def solve(self,log):
        _id,b=log.split(' ',maxsplit=1)
        if(b[0].isalpha()):
            return (0,b,_id)
        else:
            return (1,)
        
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        return sorted(logs,key=self.solve)

参考文献

[LeetCode] solution

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值