Leetcode Happy Number

Source: Internet
Author: User

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
Problem Solving Ideas:

This problem defines a number of happiness, that is, for a positive integer, if the number of its individual bits squared, and then add up to get a new number, and then do the same operation, if the final result becomes 1, then it is happy number, if the cycle is not 1, it is not the number of happiness, So now arbitrarily give us a positive integer, let us judge whether this number is happy number, the topic given in the example 19 is the number of happiness, then we look at a not happy number of cases, such as the number 11 as the next computational process:

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

We found that at the end of the figure 4 again, then the number will repeat the previous order, the loop does not contain 1, then the number 11 is not a happy number, found that the law will be considered how to implement the code, we can use a hash table to record all occurrences of the number, and then every occurrence of a new number, Look in the hash table to see if it exists, join the table if it does not exist, jump out of the loop if it exists, and determine if this number is 1, and if 1 returns true, not 1 returns false.

Java Code:

The first type:

 Public BooleanIshappy (intN) {//The key to solve this problem are to determine where to stop loop        if(n<=0) {return false; } if(n = = 1) {return true; } HashSet<Integer> result =NewHashset<integer>(); //n > 1         while(n!=1) {result.add (n); intsum = 0;  while(n>0) {sum+ = (n%10) * (n%10); N= N/10; }            if(Sum = = 1) {                return true; }            if(Result.contains (sum)) { Break; } N=sum; }        return false; }

The second kind: (Refer to others, more concise and clear)

  Public BooleanIshappy (intN) {HashSet<Integer> Happy =NewHashset<integer>(); intsum = 0;  while(!happy.contains (n)) {Sum=0;            Happy.add (n); intTMP = 0;  while(N > 0) {tmp= n% 10; N= N/10; Sum+ = tmp*tmp; }            if(Sum = = 1){                return true; } N=sum; }        return false; }

Reference:

1. http://www.cnblogs.com/grandyang/p/4447233.html

2. https://sisijava.wordpress.com/2015/05/26/leetcode-happy-number/

Leetcode 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.