• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[leet_code] 202. Happy Number with python3

03 Apr 2020

Reading time ~1 minute

문제 : https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3284/ https://leetcode.com/problems/happy-number/

  1. 4가 되면 무한루프가 나온다고 한다… ``` python

class Solution: def isHappy(self, n: int) -> bool: while n != 1: n = sum([int(i) ** 2 for i in str(n)]) if n == 4: return False

    return True ```
  1. 풀이설명: 이미 나왔던 숫자가 다시 나오면 무한루프가 나오기 때문에 set을 이용해 무한루프 확인
    def isHappy(n: int) -> bool:
     notHappy = set()
     while n != 1:
         notHappy.add(n)
         n = sum([int(i) ** 2 for i in str(n)])
         if n in notHappy:
             return False
     return True
    


algorithmleet_code30days Share Tweet +1