본문 바로가기

리트코드47

[LEET CODE] 206. Reverse Linked List # Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Constraints: The number of nodes in the list is the range [0, 5000]. -5000 ListNode: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) # Solution 2 - 반복 구조로 뒤집기 # Definition for singly-linked.. 2021. 3. 22.
[LEET CODE] 21. Merge Two Sorted Lists ( 연산자 우선순위, 변수 스왑) # Problem Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Constraints: The number of nodes in both lists is in the range [0, 50]. -100 ListNode: if (not l1) or (l2 and l1.val > l2.val): l1, l2 = l2, l1 # 작은 값을 계속 l1에 연결시킨다. if l1: l1.next = self.mergeTwoLists(l1.next, l2) return l1 # 연산자 우선순위 우선순위 연산자 설명.. 2021. 3. 22.
[LEET CODE] 234. Palindrome Linked List ( 연결리스트, 런너 기법) # Problem Given the head of a singly linked list, return true if it is a palindrome. Constraints: The number of nodes in the list is in the range [1, 105]. 0 bool: q: List = [] if not head: return True node = head # 리스트 변환 while node is not None: q.append(node.val) node = node.next # 팰린드롬 판별 # 시작과 끝 하나씩 비교 while len(q) > 1: if q.pop(0) != q.pop(): return False return True # Solution 2 - 데크를 이용한 .. 2021. 3. 22.
[LEET CODE] 49. Group Anagrams (dict 자료형, sort(), sorted()) # Problem * anagram(애너그램) : 일종의 언어유희로 문자를 재배열하여 다른 뜻을 가진 단어로 바꾸는 것 Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input : strs = ["eat","tea","tan","ate","nat","bat"] Output : [["bat.. 2021. 3. 15.
[LEET CODE] 819. Most Common Word ( 정규표현식 ) #Problem Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique. The words in paragraph are case-insensitive and the answer should be returned in lowercase. Example 1: Input : paragraph : "Bob hit a ball, the hit BALL flew far after it w.. 2021. 3. 15.
반응형