Leetcode - 937. Reorder Data in Log Files
Coding Test

Leetcode - 937. Reorder Data in Log Files

일시불

문제

Loading...
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

풀이

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        letter_log = []
        digit_log = []
        for log in logs:
            if log.split()[1].isnumeric():
                digit_log.append(log)
            else:
                letter_log.append(log)

        return (
            sorted(letter_log, key=lambda x: (x.split()[1:], x.split()[0]))
            + digit_log
        )

만일 digit_log 도 함께 정렬하는 문제였다면 아래와 같이 풀이 가능하다.

class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        def key(log):
            splited = log.split()
            identifier, content = splited[0], splited[1:]
            
            return (content[0].isnumeric(), content, identifier)
        
        return sorted(logs, key=key)