q202_快乐数

package 快慢指针遍历.q202_快乐数;
/**
 * 快慢指针,思想同q141判断是否有环,用快慢指针找出循环终止条件 o(n)
 */
public class Solution {
    private int bitSquareSum(int n) {
        int sum = 0;
        while (n > 0) {
            int bit = n % 10;
            sum += bit * bit;
            n = n / 10;
        }
        return sum;
    }
    public boolean isHappy(int n) {
        int slow = n;
        int fast = n;
        do {
            slow = bitSquareSum(slow);
            fast = bitSquareSum(fast);
            fast = bitSquareSum(fast);
        } while (slow != fast);
        return slow == 1;
    }
}