leetcode 202:Happy number

原创 2015年07月08日 13:16:36

题目:
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.

Example: 19 is a happy number

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

分析:
本地考察一个数是否为happy number,根据happy number的规定来进行解题,将给定的数按位存入数组,对数组每一项求平方再求和,最终结果若为1则返回true;在这一过程中,利用hashset判定某个数是否已经出现过,若已经出现过,则返回false.
代码:

import java.util.HashSet;
import java.util.Set;

public class happyNumber {
    public static boolean isHappy(int n){
        if(n<=0) return false;
        long ln=n;
        Set<Long> set=new HashSet<Long>();
        while(ln<=Integer.MAX_VALUE){
            if(set.contains(ln))
                return false;
            else
                set.add(ln);
            ln=digitSquare(ln);
            if(ln==1) return true;
        }
        return false;
    }
    private static long digitSquare(long ln){
        long sum=0;
        while(ln!=0){
            sum +=Math.pow(ln%10, 2);
            ln /=10;
        }
        return sum;
    }
    public static void main(String[] args){
        int n=19;
        boolean result=isHappy(n);
        System.out.println("isHappy:"+result);
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【202-Happy Number(开心数字)】

【202-Happy Number(开心数字)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题  ...

leetcode 202 Happy Number

202. Happy Number Write an algorithm to determine if a number is "happy". A happy number is a num...

Leetcode_202_Happy Number

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45396585 Write an algorith...

Happy Number - LeetCode 202

题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number defined...
  • bu_min
  • bu_min
  • 2015年05月20日 20:32
  • 207

<LeetCode OJ> 202. Happy Number

202. Happy Number My Submissions Question Total Accepted: 46878 Total Submissions: 134944 Diffic...

【leetcode】202. Happy Number

一、题目描述 Write an algorithm to determine if a number is "happy". A happy number is a number defined...

Leetcode NO.202 Happy Number

本题题目要求如下: Write an algorithm to determine if a number is "happy". A happy number is a number defi...

LeetCode 202 Happy Number(开心数)(vector、unordered_set)

翻译写一个算法来决定一个数是否是“开心”的。开心数是被如下步骤所定义的数:从所有正整数开始,用其每个数字的平方和来替代这个数,不断重复这个过程直到最后的结果为1(此时它就停止了),或者它在一个不包含1...
  • NoMasp
  • NoMasp
  • 2016年01月19日 10:51
  • 1739

Leetcode 202:Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the fo...

[leetcode]202.Happy Number

题目 Write an algorithm to determine if a number is "happy". A happy number is a number define...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 202:Happy number
举报原因:
原因补充:

(最多只允许输入30个字)