linux怎么查看本机内存大小
313
2022-08-26
[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:
0 <= logs.length <= 1003 <= logs[i].length <= 100logs[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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~