Today first talk about Freud Award ring, Freud Award ring originally is in a circle have two people running, at the same time start, one person's speed is another person's twice times, then that person can catch up with another person in the next lap, Freud sentence can solve the problem that the number will circulate, for example, judge a linked list is not a circular link list.
Leetcode Happy number is such a simple problem, there are many ways to achieve, but Freud's sentence is relatively simple, but also the high efficiency of the kind.
Class Solution {Public:int change (int n) {int ans = 0;for (; n!=0; ans+= (n%10) * (n%10), n/=10); return ans;} BOOL Ishappy (int n) {int one = N;int double = N;while (1) {one = Change (single);//double speed: Change (change);//Twice times speed if (a = = 1 || both = = 1) return true;if (one = =) return false;}};
Leetcode Happy Number Freud Award ring solution Cycle