LeetCode_202. 快乐数

public class S_202 {
    public boolean isHappy(int n) {
        Set<Long> set = new TreeSet<>();
        while(true) {
            long t = 0;
            while(n > 0) {
                // pow 平方
                t += Math.pow(n % 10, 2);
                n /= 10;
            }
            if(t == 1) return true;
            if(!set.add(t)) return false;
            n = (int) t;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/83108507