Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- there is only two possibilities, either the number will become 1 or get stuck in some loop so we can maintain a ste where after every transformation, we will push the number into the set and once our transformed number is already present in the set, we will return the False, we will do this until we our number becomes 1.
- '''
- def get_next(n):
- total = 0
- while n > 0:
- digit = n % 10
- total += digit * digit
- n //= 10
- return total
- class Solution:
- def isHappy(self, n: int) -> bool:
- seen = set()
- while n != 1:
- if n in seen:
- return False
- seen.add(n)
- n = get_next(n)
- return True
Advertisement
Add Comment
Please, Sign In to add comment