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 <= 100
- 3 <= logs[i].length <= 100
- 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)