LeetCode:Happy Number - 快乐数

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) {
        
        int temp = n;
        LinkedList<Integer> linkedList = new LinkedList<Integer>(); 
        linkedList.add(temp);
        while (true) {
            temp = getNext(temp);
            if (temp == 1) {
                return true;
            } else if (linkedList.contains(temp)) {
                return false;
            }
            linkedList.add(temp);
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private int getNext(int num) {
        int result = 0;
        while (num > 0) {
            result += (num % 10) * (num % 10);
            num = num / 10;
        }
        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) {
        
        int temp = n;
        HashSet<Integer> hashSet = new HashSet<Integer>(); 
        hashSet.add(temp);
        while (true) {
            temp = getNext(temp);
            if (temp == 1) {
                return true;
            } else if (hashSet.contains(temp)) {
                return false;
            }
            hashSet.add(temp);
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private int getNext(int num) {
        int result = 0;
        while (num > 0) {
            result += (num % 10) * (num % 10);
            num = num / 10;
        }
        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) {
        
        int temp = n;
        while (true) {
            temp = getNext(temp);
            if (temp > 243) {
                continue;
            } else if (temp == 4 || temp == 16 || temp == 37 || temp == 58 ||
                temp == 89 || temp == 145 || temp == 42 || temp == 20) {
                return false;
            } else if (temp == 1) {
                return true;
            }
        }
    }
    
    /**
     * 获取下一个快乐的数
     * @param num
     * @return
     */
    private int getNext(int num) {
        int result = 0;
        while (num > 0) {
            result += (num % 10) * (num % 10);
            num = num / 10;
        }
        return result;
    }
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/524681

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值