202. Happy number
- Total accepted:78171
- Total submissions:208635
- Difficulty:easy
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.
Example:19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
Ideas:
For a number n, if n is not happy number, the loop is bound to occur in the process of finding the sum of squares of n each digit and the sum of squares of each number after n. The relevant proof in discuss.
The original text reads as follows:
Earlier posts gave the algorithm but does not explain why it isValid mathematically, and This isWhat ThisPost isAbout:present A" Short"mathematical proof. First of all, it isEasy to argue that starting fromA number I,ifSome Value-say A-appears again during the process after K-steps, the initial number I cannot be a happy number. Because A would continuously become a after every k steps. Therefore, as Long asWe can show that there isA loop after running the process continuously, the number isNot a happy number. There isAnother detail not clarified yet:for any non-happy number, would it definitely end up with a loop during the process? This isImportant, because it isPossible forA non-happy number to follow the process endlessly whileHaving no loop. To show that a non-happy number would definitely generate a loop, we only need to show that forAny non-happy number, all outcomes during the process is bounded by some large but finite integer N. If all outcomes can is onlyinchA finiteSet(2, N], and since there is infinitely many outcomes forA non-happy number, there have to is at least one duplicate, meaning a loop!Suppose after a couple of processes, we end up with a large outcome O1 with D digitswhereD isKind of large, say d>=4, i.e., O1 >999(If we cannot even reach such a large outcome, it means all outcomes is bounded by999==> Loop exists). We can easily see this after processing O1, theNewOutcome O2 can be in most9^2*d < 100D, meaning that O2 can has at most2+d (d) Digits,whereD (d) isThe number of digits D has. It isObvious that2+d (d) < D. We can further argue that O1 isThe maximum (or boundary) of all outcomes afterwards. This can is shown by Contradictory:suppose after some steps, we reach another large number O3 > O1. This means we process on some number W <=999That yields O3. However, ThisCannot happen because the outcome of W can be in most9^2*3< -< O1.
Method One: Directly with the definition. Note, however, that there is no corresponding key-value mapping in the map, the return is 0, so it is important to determine in advance whether the key currently being mapped is 0.
Method Two: In fact, in the above proof can also be known, as long as the current number n is not 1 digits, according to 2+d (d) <d, you can know that the future number of the next number than the previous digit is to be reduced, and eventually become 1 digits. So, just find the number of happy numbers in [1,9]. (1 and 7 are happy number). As a result, you can always converge to 1 digits as long as you keep looping.
Method three: With Floyd ' s cycle-finding algorithm. The essence is 2 quick and slow hands, the fast pointer if and slowly the pointer encounters, indicates exists the ring.
Code:
Method One:
1 classSolution {2 Public:3 BOOLIshappy (intN) {4map<int,int>Thash;5 while(n&&!Thash[n]) {6thash[n]=N;7 inttemp=0, low;8 while(n) {9low=n%Ten;Tentemp+=low*Low ; OneN/=Ten; A } -n=temp; - } the if(n==1){ - return true; - } - return false; + } -};
Method Two:
1 classSolution {2 Public:3 intNextintN) {4 inttemp=0, low;5 while(n) {6low=n%Ten;7temp+=low*Low ;8N/=Ten;9 }Ten returntemp; One } A BOOLIshappy (intN) { - while(n>9){ -n=next (n); the } - if(n==1|| n==7){ - return true; - } + return false; - } +};
Method Three:
1 classSolution {2 Public:3 intNextintN) {4 inttemp=0, low;5 while(n) {6low=n%Ten;7temp+=low*Low ;8N/=Ten;9 }Ten returntemp; One } A BOOLIshappy (intN) { - intFast=next (n), slow=N; - while(fast!=slow) { theslow=next (slow); -fast=next (fast); - } - if(fast==1){ + return true; - } + return false; A } at};
Leetcode 202. Happy number