본문 바로가기

알고리즘/LeetCode

Remove Duplicate Letters LeetCode의 Remove Duplicate Letters 문제다. Remove Duplicate Letters - 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 문제가 조금 난해할 수 있는데 주어진 문자열에 대해서 모든 문자가 단 한 번만 등장해야 한다. 그러면서도 최대한 알파벳 순으로 가장 작은 문자열로 만들어야 한다. 문제에서 주어진 예시는 "bcabc"를 "abc"로 만들었다. 왜냐면 이 문자열에서는 'b'와 'c'가 중복되는데 중복을 제거하면 최종적.. 더보기
Reverse Linked List II LeetCode의 Reverse Linked List II 문제다. Reverse Linked List II - 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 첫 번째 노드부터 마지막 노드까지 오름차순 값을 가진 노드들의 연결 리스트를 주어진 범위만큼 뒤집어서 반환하는 것이다. 예를 들어 노드 1, 노드 2, 노드 3, 노드 4, 노드 5가 있을 때 2번째 노드부터 4번째 노드까지 뒤집어서 반환하라고 하면 노드 1, 노드 4, 노드 3, 노드 2, 노드 5 순서.. 더보기
Odd Even Linked List (Linked List) LeetCode의 Odd Even Linked List 문제다. Odd Even Linked List - 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 연결 리스트가 주어졌을 때 홀수 번째의 노드와 짝수 번째의 노드끼리 묶어서 홀수 노드들 다음에 짝수 노드들이 오도록 재구성하는 문제다. 즉 노드 1, 노드 2, 노드 3, 노드 4, 노드 5가 있다면 노드 1, 노드 3, 노드 5, 노드 2, 노드 4로 다시 구성돼야 한다. 간단하게 생각한다면 그냥 연결 리스트를.. 더보기
Swap Nodes in Pairs (Linked List) LeetCode의 Swap Nodes in Pairs 문제다. Swap Nodes in Pairs - 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 연결 리스트로 주어진 노드를 2개씩 swap, 즉 서로 위치를 바꾸는 것이 문제의 목적이다. 예를 들어 노드가 1 -> 2 -> 3 -> 4처럼 주어진 연결 리스트가 있을 때 2 -> 1 -> 4 -> 3처럼 한 쌍씩 바꿔야 하며 만약 홀수개의 노드만 있다면 바꾸지 않는다. 즉 1 -> 2 -> 3 은 2 -> 1 .. 더보기
Add Two Numbers (Linked List) LeetCode의 Add Two Numbers 문제다. Add Two Numbers - 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 연결 리스트로 주어진 역순 숫자들을 각각 합해서 동일하게 역순 숫자들의 연결 리스트로 반환하는 문제다. 이때 리스트의 각 숫자들은 숫자 문자열의 각 자릿수를 역순으로 나열한 것인데 아래와 같은 구조로 주어진다. 예를 들어 [2, 4, 3]의 리스트(연결 리스트지만 일반 리스트로 표현한다)와 [5, 6, 4]의 리스트가 주어지면 [.. 더보기
Reverse Linked List (Linked List) LeetCode의 Reverse Linked List 문제다. Reverse Linked List - 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 단방향 연결 리스트를 뒤집는 기능을 구현하는 문제다. 별로 어려운 문제는 아닌데 다음처럼 파이썬의 다중 할당(multiple assignment)을 사용해서 간단하게 풀 수 있어서 포스팅으로 남긴다. class Solution: def reverseList(self, head: ListNode) -> ListNode.. 더보기
Merge Two Sorted Lists (Linked List) LeetCode의 Merge Two Sorted Lists 문제다. Merge Two Sorted Lists - 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 두 개의 정렬된 연결 리스트를 받아 하나의 정렬된 리스트로 합치는 문제다. 두 연결 리스트는 정렬되어 있기 때문에 단순히 두 리스트 요소들을 비교하면서 결과 리스트에 삽입하는 방식으로 구현하면 다음과 같다. class Solution: def mergeTwoLists(self, l1: ListNode, l.. 더보기
Palindrome Linked List (Linked List) LeetCode의 Palindrome Linked List 문제다. Palindrome Linked List - 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 회문(Palindrome)을 구하는 문제인데 이번에는 단방향 연결 리스트를 활용하고 있다. 가장 먼저 생각해볼 수 있는 방법은 연결 리스트를 리스트로 바꿔서 거꾸로 뒤집어서 비교해보는 것이다. class Solution: def isPalindrome(self, head: ListNode) -> bool:.. 더보기