Happy Number
Easy · Hash Table, Math, Two Pointers
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Return true if n is a happy number, and false if not.
Example 1
Input: n = 19 Output: true
12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1
Example 2
Input: n = 2 Output: false
Constraints
- 1 <= n <= 231 - 1
Starter code (python)
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
Related problems
- Two Sum (Easy)
- Add Two Numbers (Medium)
- Longest Substring Without Repeating Characters (Medium)
Previous: Bitwise AND of Numbers Range · Next: Remove Linked List Elements · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.