๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
937. reorder-data-in-log-files | 56.4% | Easy |
Reorder Data in Log Files - LeetCode
Can you solve this real interview question? Reorder Data in Log Files - You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier. There are two types of logs: * Letter-logs: All words (except the
leetcode.com
๋ฌธ์ ์์ฝ. ๋ก๊ทธ ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ ๋ ฌํด๋ผ.
์กฐ๊ฑด 1) ๋ก๊ทธ ๋ฐฐ์ด์ด ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค
์กฐ๊ฑด 2) ๊ฐ ๋ก๊ทธ๋ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถ๋๋ค
์กฐ๊ฑด 3) ์ฒซ ๋ฒ์งธ ๋จ์ด๋ ์๋ณ์์ด๋ค
์กฐ๊ฑด 4) ๋ก๊ทธ์๋ Letter-log(์๋ฌธ์๋ก ๊ตฌ์ฑ), Digit-log(์ซ์๋ก ๊ตฌ์ฑ) ๋ ๊ฐ์ง๊ฐ ์๋ค
์กฐ๊ฑด 5) Letter-log๋ ๋ชจ๋ Digit-log ์์ ์์นํ๋ค
์กฐ๊ฑด 6) Letter-log๊ฐ ๋์ผํ ๊ฒฝ์ฐ ์๋ณ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค
์กฐ๊ฑด 7) Digit-log๋ ์ ๋ ฅ ์์๋๋ก ์ ๋ ฌํ๋ค
Step1. Letter-log, Digit-log ์๋ณ -> split(), isalpha(), isdigit() ์ด์ฉ
๋ฌธ์์ด์ seperator ๊ธฐ์ค์ผ๋ก ๋๋ ๋ง์ฝ, maxsplit์ ์ง์ ํ ๊ฒฝ์ฐ ๋๋ ํ์ ์ ์ฝ์ด ์กด์ฌ |
string.split(seperator, maxsplit) |
๋ฌธ์์ด์ ๋ชจ๋ ๋ฌธ์๊ฐ ์์ด๋ ํ๊ธ์ธ์ง ํ์ธํ๋ ํจ์ | string.isalpha() |
๋ฌธ์์ด์ ๋ชจ๋ ๋ฌธ์๊ฐ ์์ด๋ ํ๊ธ์ด๋ ์ซ์์ธ์ง ํ์ธํ๋ ํจ์ | string.isalnum() |
Step2. Letter-log ์ ๋ ฌ -> sort() ํจ์ ์ด์ฉ
๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌ๋ ์ํ๋ก ๋ณ๊ฒฝ(๋ฐํ X) | list.sort(key=None, reverse=False) |
์ดํฐ๋ฌ๋ธ ๊ฐ์ฒด๋ก๋ถํฐ ์ ๋ ฌ๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑ(๋ฐํ O) | list.sorted(key=None, reverse=False) |
- key ๋งค๊ฐ๋ณ์
ํน์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ ์ํด ์ฌ์ฉ
์ฃผ๋ก lambda x ํจ์๋ฅผ ์ด์ฉ, (๊ดํธ)๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฌ ์์ ์ง์ : ์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ์ ์์ ๊ฒฝ์ฐ ๋ค ๋ด์ฉ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
- reverse ๋งค๊ฐ๋ณ์
key ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ธ์ง ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ธ์ง ์ค์ ํ ๋ ์ฌ์ฉ(๊ธฐ๋ณธ์ ์ค๋ฆ์ฐจ์)
Step3. Letter-log. Digit-log ํฉ์น๊ธฐ -> ๋ฐฐ์ด ๋ง์ ์ด์ฉ
๊ตฌํ
class Solution(object):
def reorderLogFiles(self, logs):
"""
:type logs: List[str]
:rtype: List[str]
"""
letter_logs=[]
digit_logs=[]
#Step1
for log in logs:
if(log.split()[1].isalpha()):
letter_logs.append(log)
else:
digit_logs.append(log)
#Step2
letter_logs.sort(key=lambda x : (x.split()[1:], x.split()[0]))
#Step3
return letter_logs+digit_logs
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 5. longest_palindromic_substring (Medium) 2023/4/28 (0) | 2023.04.28 |
---|---|
[LeetCode] 49. group-anagrams (Medium) 2023/4/28 (0) | 2023.04.28 |
[LeetCode] 344. reverse-string (Easy) 2023/4/27 (0) | 2023.04.27 |
[LeetCode] 125. valid-palindrome (Easy) 2023/4/27 (0) | 2023.04.27 |
[python] 1. ์ฝ๋ฉํ ์คํธ์ ํ์ ์ฒดํฌ ๋ชฉ๋ก (0) | 2023.04.26 |