> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_2_math/problem-202-happy-number.md).

# Problem 202: Happy Number

> <https://leetcode.com/problems/happy-number/#/description>

## 思路

* 用一个 set 来记录是否之前已经存过这个值，可以减少重复运算

```java
public class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<Integer>();
        set.add(n);
        while (n != 1) {
            int sum = 0;
            while (n != 0) {
                sum += Math.pow(n % 10, 2);
                n /= 10;
            }
            if (sum == 1) return true;
            if (set.contains(sum)) return false;

            set.add(sum);
            n = sum;
        }

        return true;
    }
}
```
