IT/알고리즘73 [Leetcode]316. Remove Duplicate Letters Remove Duplicate Letters - LeetCode 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 풀이 1 : 재귀 어떤 방식으로 풀어야 할지도 감이 안잡히는 문제였다. 재귀를 이용한 풀이를 보고 감탄하지 않을 수 없었다. 전제 문자열을 set으로 변경한 후 정렬을 해주고 순회하면서 suffix = s[s.index(char):] 을 통해 사전식 출력에 대한 문제를 해결하는 부분이 너무 신기했다. de.. 2020. 10. 6. [Leetcode]739. Daily Temperatures 브루트 포스로 풀면 시간초과가 나는 문제였다. 스택을 이용하는 문제라는 힌트는 봤는데 어떤 방식으로 문제를 풀어야할지 감이 잡히지 않았다. 스택으로 인덱스를 저장하는 아이디어를 이용했다. 스택으로 인덱스를 저장하면서 현재 값보다 작은 값을 가지는 인덱스가 스택에 존재하면 pop을 하면서 ans 배열에 그 인덱스의 차를 저장하여 푸는 문제였다. def dailyTemperatures(self,T:List[int])->List[int]: ans = [0 for i in range(len(T))] stack = [] for i,cur in enumerate(T): while stack and T[stack[-1]] < cur: idx = stack.pop() ans[idx] = i - idx stack.app.. 2020. 10. 6. [Leetcode]20. Valid Parentheses Valid Parentheses - LeetCode 가장 기본적인 문제였다. 풀이를 보니 딕셔너리를 이용해서 해결한 것 같은데 집에 가면 책을 참고하면서 다시 풀이를 확인해야겠다. 내 풀이 (분명히 저번에 풀어본 문젠데 코드가 상당히 더럽게 나왔다....) def isValid(self, s: str) -> bool: stack = [] for c in s: if c == '(' or c == '{' or c == '[': stack.append(c) elif stack: if c == ')' and '(' == stack[-1]: stack.pop() elif c == '}' and '{' == stac.. 2020. 10. 6. [Leetcode]234. Palindrome Linked List Palindrome Linked List - LeetCode 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 데크를 이용한 풀이 리스트로 변환하여 pop(0)를 이용해 문제를 해결할 수 있지만 최적화를 위해 deque 를 사용 def isPalindrome2(self,head:ListNode)->bool: q : Deque = collections.deque() if not head: return True node = .. 2020. 9. 25. 이전 1 2 3 4 5 6 ··· 19 다음