문제 링크: https://leetcode.com/problems/reorder-data-in-log-files
Reorder Data in Log Files - LeetCode
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....
leetcode.com
내 풀이:
![[LeetCode] Reorder Data In Log Files [LeetCode] Reorder Data In Log Files](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
참고용 풀이:
![[LeetCode] Reorder Data In Log Files [LeetCode] Reorder Data In Log Files](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
Review:
내 풀이와 참고용 풀이의 풀이 방식은 거의 비슷한데, 나는 숫자 로그 배열을 정렬된 배열에 연결하기 위해 extend() 함수를 이용했고, 참고용 풀이에서는 +를 사용해 배열을 연결했다. 이 문제를 통해 + 연산자를 사용해 배열을 연결할 수 있는 것은 배웠다.
Idea:
1. List logs의 원소를 하나씩 읽으며 split()으로 나누고 첫 번째 원소가 알파벳이면 배열 A(문자 로그)에 append하고, 알파벳이 아니라면 숫자 로그이므로 배열 B(숫자 로그)에 append한다.
2. 람다를 이용한 정렬을 통해 식별자를 제외한 문자들로 정렬하고, 문자가 동일한 경우가 있을 수 있으므로 식별자를 이용해 추가로 정렬한다.
3. 정렬된 배열 A에 숫자 로그로 구성된 B의 원소를 붙인다.
* Key Point: 문자 로그와 숫자 로그를 나누고, 문자 로그를 먼저 정렬한 뒤, 숫자 로그를 정렬된 배열에 이어붙인다.
'알고리즘 문제 풀이: 파이썬 > LeetCode' 카테고리의 다른 글
[LeetCode] The K Weakest Rows in a Matrix (0) | 2022.05.01 |
---|---|
[LeetCode] Most Common Word (0) | 2022.03.08 |
[LeetCode] Reverse String (0) | 2022.03.08 |
[LeetCode] Valid Palindrome (0) | 2022.03.07 |
댓글