LeetCode7 ❗️[LEET CODE] 687. Longest Univalue Path # Problem Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass through the root. The length of the path between two nodes is represented by the number of edges between them. Constraints: The number of nodes in the tree is in the range [0, 104]. -1000 2021. 4. 20. [LEET CODE] 20. Valid Parentheses # Problem Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Constraints: 1 bool: stack = [] table = { ')': '(', '}': '{', ']': '[', } # 스택 이용 예외 처리 및 일치 여부 판별 for char in s: if char not in.. 2021. 3. 23. [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. 이전 1 2 다음 반응형