其他分享
首页 > 其他分享> > 快乐数

快乐数

作者:互联网

快乐数

class Solution {
    private int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    public boolean isHappy(int n) {
        Set<Integer> seen = new HashSet<>();
        while (n != 1 && !seen.contains(n)) {
            seen.add(n);
            n = getNext(n);
        }
        return n == 1;
    }
}

标签:getNext,10,return,int,totalSum,快乐,seen
来源: https://blog.csdn.net/xiaomagezuishuai/article/details/114632621