본문 바로가기

leetcode 풀이20

leetcode 94. Binary Tree Inorder Traversal 전형적인 트리순회에 대한 문제이며, 순회하는 방법은 inorder이다. 재귀방식과 iterate한 방식이 있는데, 순회방법은 stack을 이용하면 좋다 class TreeNode(object): def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution(object): def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ res = [] stack = [] curr = root while stack or root: if curr : stack.append(curr) c.. 2023. 1. 22.
leetcode 28. Find the index of the First Occurence in a String https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/ 미디움이라고 하지만, 전혀 미디움 난이도가 아닌 문제 class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if needle in haystack : return haystack.index(needle) return -1 2023. 1. 21.
leetcode 24. Swap Nods in Pairs def swap(head): dummy = ListNode(0) dummy.next = head prev, curr = dummy, head while curr and curr.next: ## save prt next_ptr = curr.next.next second = curr.next ## reverse second.next = curr curr.next = next_ptr prev.next = second ## update ptrs prev = curr curr = next_ptr return dummy.next https://www.youtube.com/watch?v=o811TZLAWOo 2023. 1. 21.
boj-1931. 회의실배정 def fn(x): return x[0] mettings = [] for _ in range(int(input())): start, end = map(int, input()) mettings.append((end, start)) mettings = sorted(mettings, key=fn) t = 0 mettings_cnt = 0 for end, start in mettings: if t 2023. 1. 20.
boj-5397. 키 로거 import sys for _ in range(int(input())): left = [] right = [] input = sys.stdin.readline() for c in input : if c == "": if right : left.append(right.pop()) elif c == "-": if left : left.pop() else : left.append(c) left.extend(right) print("".join(list(left))) 2023. 1. 20.
boj-7785. 회사에있는사람 import sys input = sys.stdin.readline employee = set() for _ in range(int(input())): name, status = input().split() if status == "enter": employee.add(name) else: employee.remove(name) for name in sorted(employee, reverse=True): print(name) 2023. 1. 20.
boj-2164. 카드 2 import collections N = 6 queue = collections.deque([i for i in range(1, N+1)]) print(list(queue)) print("-"*5) while queue : tmp = queue.popleft() last_num = queue.popleft() queue.append(last_num) # print(list(queue)) if len(queue) == 1: print(queue[0]) break 2023. 1. 19.
boj-11866. 요세푸스 문제 import collections N, K = 7, 3 queue = collections.deque([i for i in range(1, N+1)] ) # print(list(queue)) joshep = [] while queue : for i in range(K-1): tmp = queue.popleft() queue.append(tmp) dead = queue.popleft() joshep.append(dead) print(joshep) 2023. 1. 19.
leetcode 21. Merge Two Sorted Lists # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def mergeTwoLists(self, list1, list2): """ :type list1: Optional[ListNode] :type list2: Optional[ListNode] :rtype: Optional[ListNode] """ if list1 is None: return list2 if list2 is None : return list1 head = None if list1.val < list2.v.. 2022. 8. 28.
[기본 알고리즘] Binary Search print('Hello, world!') l = [1,2,3,4,5,6,7,8,9,10] target = 4 def binarySearch(l): left = 0 right = len(l) - 1 step = 0 while left target: right = pivot - 1 elif l[pivot] < target: left = pivot + 1 else : return pivot return -1 print(binarySearch(l)) 재귀 / 반복문으로 풀 수 있지만, 이왕이면 재귀를 지양하자는 의미에서 반복문으로만 풀어봄 2022. 8. 28.