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
Credits:
Special thanks to @mithmatt and @ts for adding the problem and creating all test cases.
Test instructions: Given an integer, determine if it is happy number.
Happy number definition: Computes the sum of squares of numbers on the individual bits of an integer, repeats the process, and if present and 1, indicates that the count is Happy. If there is an infinite loop in a digital circle, and the number circle does not include 1, then the number is not happy.
Method One: Start to understand the wrong test instructions, think of it as if a number is not happy, then there will be an infinite loop of squares and always a value. After the submission does not pass and read the question again, it is found that if a number is not happy, then the square will start to circulate from somewhere (think about it too).
Using the set cannot hold the attributes of the repeating element, the sum of squares is stored in set each time, if the sum of squares and sums in the set has already appeared, then it has entered the loop, which is not a happy number.
Beats 18.50% of Java submissions.
Public BooleanIshappy (intN) {intsum = 0, temp; Set<Integer> set =NewHashset<>(); while(!set.contains (n)) {Set.add (n); while(n! = 0) {Temp= n% 10; Sum+ = temp *temp; N= N/10; } if(Sum = = 1) return true; N=sum; Sum= 0; } return false; }
Method Two: The sum of squares of each calculation is deposited into a list, using the fast pointer, if the quick pointer with the slow pointer reunion, then there is a ring, which is not happy number.
Beats 78.05% of Java submissions.
Public BooleanIshappy (intN) {intsum = 0, temp; Node Head=NewNode (n); Node Fast= head, slow = head, test =Head; while(slow = = Test | | Fast.val! = slow.val) {//slow = = Test to exclude both fast and slow from the first first node while(n! = 0) {Temp= n% 10; Sum+ = temp *temp; N= N/10; } if(Sum = = 1) return true; N=sum; Sum= 0; Head.next=NewNode (n); Head=Head.next; if(Fast.next.next! =NULL) {Fast=Fast.next.next; Slow=Slow.next; } } return false; } classnode{intVal; Node Next; PublicNode (intval) { This. val =Val; } }
Method Three: Just different from the loop end condition of method two
Public BooleanIshappy (intN) {intsum = 0, temp; Node Head=NewNode (n); Node Fast= head, slow = head, test =head;; while(N! = 1){ while(n! = 0) {Temp= n% 10; Sum+ = temp *temp; N= N/10; } N=sum; Sum= 0; Head.next=NewNode (n); Head=Head.next; if(Fast.next.next! =NULL) {Fast=Fast.next.next; Slow=Slow.next; } if(Slow! = Test && Fast.val = =slow.val)return false; } return true; }
Method Four: Use the function to replace the fast and slow pointer, refer to: https://www.jianshu.com/p/f7b632e31d5f
Beats 78.05% of Java submissions.
Public BooleanIshappy (intN) { intsum = 0; intFast = N, slow =N; while(Sum! = 1) {Slow=getsum (slow); if(Slow = = 1) return true; Fast=Getsum (getsum (fast)); if(Fast = =slow)return false; Sum=slow; } return true; } Public intGetsum (intN) { inttemp, sum = 0; while(n! = 0) {Temp= n% 10; Sum+ = temp *temp; N/= 10; } returnsum; }
Leetcode 202. Happy number