cycle2 [LeetCode] Happy Number 파이썬 (dict, cycle) class Solution: def isHappy(self, n): lookup = {} while n != 1 and n not in lookup: lookup[n] = True n = self.nextNumber(n) return n == 1 def nextNumber(self, n): new = 0 for char in str(n): new += int(char)**2 return new 출처: github.com/jiapengwen/LeetCode/blob/master/Python/happy-number.py 2021. 4. 4. [LeetCode] Linked List Cycle 파이썬 (is operator, id) 2가지 풀이법(1: fast & slow, 2: marked as read) 풀이 1 (fast, slow) class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: # @param head, a ListNode # @return a boolean def hasCycle(self, head): fast, slow = head, head while fast and fast.next: fast, slow = fast.next.next, slow.next if fast is slow: return True return False 어떻게 풀어야 할지 전혀 감이 안왔던 문제 fast, slow라는 2개의 head 의 c.. 2021. 4. 1. 이전 1 다음