码迷,mamicode.com
首页 > 移动开发 > 详细

LeetCode:Happy Number - 快乐数

时间:2015-11-02 00:23:11      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:

1、题目名称

Happy Number(快乐数)

2、题目地址

https://leetcode.com/problems/happy-number/

3、题目内容

英文:

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 will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

中文:

设计一个算法,判断一个数字是否“快乐”。快乐数可以被如下流程定义:一个正整数,计算出它各位数字的平方和,得到一个新的数字,再对这个新的数字重复这一过程,直到最后得到数字1或是其他某几个数字的无限循环。在这些数字中,经过上述流程最终能得到数字1的数字,被称为“快乐数”。

4、解题方法

根据题目中对快乐数的定义,可以设计下面的Java代码:

import java.util.LinkedList;

/**
 * @功能说明:LeetCode 202 - Happy Number
 * @开发人员:Tsybius2014
 * @开发时间:2015年11月1日
 */
public class Solution {
    
    /**
     * 快乐的数
     * @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) {
            temp = getNext(temp);
            if (temp == 1L) {
                return true;
            } else if (linkedList.contains(temp)) {
                return false;
            }
            linkedList.add(temp);
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private Long getNext(long num) {
        long result = 0L;
        while (num > 0) {
            result += (num % 10L) * (num % 10L);
            num = num / 10L;
        }
        return result;
    }
}

这段代码中,使用链表(LinkedList)记录已经找到的数字,如果某一个生成的数字在之前的计算中曾经出现过且不为1,则说明这个数字不是快乐数,如果某一个生成的数字为1,则说明这个数字一定是快乐数。除了链表外,还可以使用HashSet记录已经找到的数字:

import java.util.HashSet;

/**
 * @功能说明:LeetCode 202 - Happy Number
 * @开发人员:Tsybius2014
 * @开发时间:2015年11月1日
 */
public class Solution {
    
    /**
     * 快乐的数
     * @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);
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private Long getNext(long num) {
        long result = 0L;
        while (num > 0) {
            result += (num % 10L) * (num % 10L);
            num = num / 10L;
        }
        return result;
    }
}

5、解题方法的改进

如果要对上面的解题方法进行改进,我们需要进一步了解快乐数的性质。

为此我查阅了维基百科上关于快乐数的描述: https://en.wikipedia.org/wiki/Happy_number

发现快乐数有如下特征:

1、如果一个数“不快乐”,则它计算到后面必然陷入到这个循环里:4, 16, 37, 58, 89, 145, 42, 20, 4, ...

2、对于一个大于243的数字来说,它的下一个数字一定比它小。这是因为一个大于1000的数字,它的下一个数字一定比它小,而对于1000以下最大的数字999,它的下一个数字是243,所以1000以下数字的下一个数字也只可能小于或等于243

基于这两个特征,我们可以设计出下面这个计算效率更高的算法:

/**
 * @功能说明:LeetCode 202 - Happy Number
 * @开发人员:Tsybius2014
 * @开发时间:2015年11月1日
 */
public class Solution {
    
    /**
     * 快乐的数
     * @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;
            }
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private Long getNext(long num) {
        long result = 0L;
        while (num > 0) {
            result += (num % 10L) * (num % 10L);
            num = num / 10L;
        }
        return result;
    }
}

END

LeetCode:Happy Number - 快乐数

标签:

原文地址:http://my.oschina.net/Tsybius2014/blog/524681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!