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;
}
}
q202_快乐数
作品《LeetCode题目分类与面试问题整理 - q202_快乐数》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/dqn.html