<span id="Label3"></p><p><p><strong>1. title</strong></p></p> <p><p>Happy number (happy)<br></p></p> <p><p><strong>2. Address of the topic</strong></p></p> <p><p>https://leetcode.com/problems/happy-number/<br></p></p> <p><p><strong>3. Topic content</strong></p></p> <p><p>English:</p></p> <p><p>Write an algorithm to determine if a number is "happy".</p></p> <p><p>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.</p></p> <p><p>Chinese:</p></p> <p><p>Design an algorithm to determine whether a number is "happy". Happiness can be defined by the following process: a positive integer that calculates the sum of squares of its numbers, gets a new number, and repeats the process for the new number until it finally gets the infinite loop of the number 1 or some other number. In these figures, after the above process can finally get the number 1, known as "happy number."<br></p></p> <p><p><strong>4. Methods of Solving problems</strong></p></p> <p><p>The following Java code can be designed according to the definition of happiness number in the Topic:</p></p> <pre class="brush:java;toolbar: true; auto-links: false;">import java.util.linkedlist;/** * @ function Description: leetcode 202 - happy number * @ developer: tsybius2014 * @ Development date: November 1, 2015 */public class Solution { /** * the number of happy * @param n * @return */ public boolean ishappy (int n) { long num = new long (n); long temp = num; LinkedList<Long> linkedList = new LinkedList<Long> (); linkedlist.add (temp); while (true) &NBSP;{&NBsp; temp = getnext (temp); if (temp == 1l) { return true; } else if ( Linkedlist.contains (temp)) { return false; } linkedlist.add (temp); } } /** * get the next happy number * @param num * @returN */ private long getnext (long num) { long result = 0L; while (num > 0) { result += (num % 10l) * (num % 10l); num = num / 10L; } return result; }}</pre> <p><p> In this code, using the linked list (linkedlist) to record the number that has been found, if a generated number in the previous calculation has occurred and is not 1, then this number is not the number of happiness, if one of the generated number is 1, then this number must be happy number. In addition to the linked list, you can use HashSet to record numbers that have been found: </p></p> <pre class="brush:java;toolbar: true; auto-links: false;">import java.util.hashset;/** * @ function Description: leetcode 202 - happy number * @ developer: tsybius2014 * @ Development date: November 1, 2015 */public class Solution { /** * the number of happy * @param n * @return */ public boolean ishappy (int n) { long num = new long (n); long temp = num; HashSet<Long> hashSet = new HashSet<Long> (); hashset.add (temp); while ( True) { temp = getnext (temp); if (temp == 1l) { return true; } else if (hashset.contains (temp)) { return false; } hashset.add (temp); } } /** * Get the next happy number * @param num * @ Return &nBsp; */ private long getnext (long num) { long result = 0L; while (num > 0) { result += (num % 10l) * (num % 10l); num = num / 10L; } return result; }}</pre> <p><p><strong>5, the improvement of solving methods</strong></p></p> <p><p>If we want to improve the method above, we need to learn more about the nature of the happiness number.</p></p> <p><p>So I looked at Wikipedia's description of the number of pleasures: Https://en.wikipedia.org/wiki/Happy_number<br></p></p> <p><p>Discover the following characteristics of Happiness:</p></p> <p><p>1, if a number "unhappy", then it calculates to the back must be caught in the loop: 4, 16, 37, 58, 89, 145, 42, 20, 4, ...<br></p></p> <p><p>2, for a number greater than 243, its next number must be smaller than it. This is because a number greater than 1000, its next number must be smaller than it, and for 1000 the largest number 999, its next number is 243, so the next number of 1000 the following number may be less than or equal to 243<br></p></p> <p><p>Based on these two characteristics, we can design the following more efficient algorithm:</p></p> <pre class="brush:java;toolbar: true; auto-links: false;">/** * @ function description: leetcode 202 - happy number * @ developer: Tsybius2014 * @ Development Time: November 1, 2015 */public class Solution { /** * Happy numbers * @param n * @return */ public Boolean ishappy (int n) { long num = new long (n); long temp = num; while (true) { temp = getnext (temp); if (temp > 243l) { continue; } else if (temp == 4l | | temp == 16l | | temp == 37l | | temp == 58l | | temp == 89l | | temp == 145l | | temp == 42l | | temp == 20l) { return false; } else if (temp == 1l) { return true; } } } /** * get the next happy number * @param num * @return */ private long getnext (Long num) { long result = 0L; while (num > 0) { result += (num % 10l) * (num % 10l); num = num / 10L; } return result; }}</pre> <p><p><strong>END</strong><br></p></p><p><p>Leetcode:happy number-happiness</p></p></span>
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