190. Happy Number
Easy · Math
A happy number is defined by the following process: starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Given a positive integer n, determine whether it is a happy number. Return true if n is a happy number, false otherwise.
Examples
Example 1 Input: n = 7 Output: true Explanation: 7 → 49 → 97 → 130 → 10 → 1. The process reaches 1, so 7 is a happy number.
Example 2 Input: n = 2 Output: false Explanation: 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. The process enters a cycle (4 appears twice), so 2 is not a happy number.
Constraints
- Standard input/output constraints apply