[leetcode]202. Happy number

Source: Internet
Author: User

Title Description:

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,
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 endlessly 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

1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

Ideas:

Idea: A happy number refers to the sum of the squares of the numbers and adds up, counting, and finally converging to 1. For a positive integer, if the numbers on each bit are squared apart,
And then add up to get a new number, and then do the same thing, if the final result becomes 1, then it is happy number, if the cycle is not 1, it is not the number of happiness, otherwise it will fall into the cycle of death.
Give an integer that determines whether it is a happy number.
If not the number of happiness. such as 11, the calculation process is as follows:
1^2 + 1^2 = 2
2^2 = 4
4^2 = 16
1^2 + 6^2 = 37
3^2 + 7^2 = 58
5^2 + 8^2 = 89
8^2 + 9^2 = 145
1^2 + 4^2 + 5^2 = 42
4^2 + 2^2 = 20
2^2 + 0^2 = 4
Will fall into a 4 cycle of death.
You can use set to record all occurrences of the number, and then each occurrence of a new number, in the set to find out if there is a table, if it does not exist,
If present, jump out of the loop and determine if this number is 1, if 1 returns true, not 1 returns false
Duplicate object does not exist in set
First determine if it is less than 0. Less than 0 returns false
When n is not equal to 1 o'clock, N is stored in the set, and the sum of squares of the N calculation numbers is recorded as the new N. Determine if n is present in set, and if so, it is in the loop. Returns false
Otherwise, n is stored in set to continue judging the new n

The second method is:
The non-happy number must appear in the Loop 4, just to determine if n appears in the loop is 4, the occurrence is false

1  Public classSolution202 {2      Public BooleanIshappy (intN) {3Hashset<integer> set =NewHashset<> (32);4         if(n <= 0)return false;5          while(n!=1){6             intTMP = 0;7 Set.add (n);8              while(n>0){9tmp+= (n%10) * (n%10);Tenn/=10; One             } An =tmp; -             if(Set.contains (n)) Break; -             Else { the Set.add (n); -             } -         } -         returnn = = 1; +         /*the second method: Determine whether the loop appears 4 can - While (n! = 1 && n! = 4) { + int t = 0; A While (n>0) { at T + = (n) * (n); - n/= Ten; -             } - n = t; -         } - return n = = 1; in         */ -     } to      Public Static voidMain (string[] args) { +         //TODO auto-generated Method Stub -Solution202 solution202 =NewSolution202 (); the         intn = 19; * System.out.println (Solution202.ishappy (n)); $     }Panax Notoginseng  -}

[leetcode]202. Happy number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.