`

Happy Number

阅读更多
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

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

题目中规定一个happy number的规则,我们借助于哈希表存储已经出现过的数字,如果计算的结果为1我们就返回true;如果计算出的结果不为1并且哈希表中已经存在这个数,就说明了它无限循环,我们返回false。代码如下:
public class Solution {
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<Integer>();
        int tem = n;
        while(true) {
            if(tem == 1) return true;
            if(set.contains(tem)) {
                return false;
            } else {
                set.add(tem);
                tem = getSquare(tem);
            }
        }
    }
    public int getSquare(int n) {
        int result = 0;
        while(n > 0) {
            result += (n % 10) * (n % 10);
            n /= 10;
        }
        return result;
    }
}
分享到:
评论

相关推荐

    leetcode202-LeetCode202_HappyNumber:判断一个数是否为happynuumber,所谓happynumber是

    LeetCode202_HappyNumber 判断一个数是否为happy nuumber,所谓happy number是指一个数,将其替换为其各位数字的平方和,重复这个过程,如果最终能得到1,这就是happy number;如果陷入一个不包含1的循环中,就不是...

    happy number

    高兴数的定义是 各个位上数的平方和等于1 比如100,10等

    hw1.rar_Happy!

    Homework 1. Helps to find happy number and consecutive numbers

    cpp-算法精粹

    Happy Number Ugly Number Ugly Number II Super Ugly Number Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse Integer Palindrome Number Insert Interval Merge Intervals ...

    LeetCode最全代码

    The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...

    LeetCode判断字符串是否循环-SimpleAlgorithm:简单算法

    判断是否为HappyNumber,输入一个数,将该数的各个位数进行平方,然后求和,再取各个位数进行平方,再求和,直到为1(此时平方求和不再改变),则为HappyNumber。如果结果是某些无限循环的非1的值,则不是Happy...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 ...

    Note-Java:Java的有关算法和数据结构的说明。 该项目包含200多个单个Java程序

    项目树 . ├── List.sh ├── README.md ├── Readme.sh ├── _Algorithm │ └── src ...│ │ ├── _202_HappyNumber.java │ │ ├── _279_PerfectSquares.java │ │ ├── _2

    OneLeetcodeADay:一天一次leetcode,并学习golang btw

    04-23从Alphabet到IntegerMapping的DecryptString 2020-04-25合并两个已排序的NodeList Fibonacci 2020-04-26查找ContinuousSequence MaximunNum 2020-04-28递归多态2020-04-30 HappyNumber 2020-05-01 AddBinary ...

    leetcode不会-ts-prep::puzzle_piece:一个TypeScriptstarter,用于通过测试驱动开发解决LeetCode问题

    leetcode 不会准备工作 :puzzle_piece: 用于通过测试驱动开发解决 LeetCode 问题的 TypeScript ...Happy Number ' 关于这个脚本这个 repo 带有一个脚本,可以为新的 LeetCode 问题快速设置启动文件

    leetcode跳跃-algorithm:leetcode

    happyNumber array 155 minStack stack 111 二叉树的最小深度 , tree 110 平衡二叉树 tree 104 二叉树的最大深度 tree 100 相同的树 tree 94 二叉树的中序遍历 tree 70 爬楼梯 dp 53 最大子序和 dp 26 删除排序数组...

    validnumberleetcode自动机-LearingTripofNoah:我自己的学习之旅,天天学习至此方休

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)! 2. 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 实现两个有序的链表合并为一个有序链表 实现求链表的中间结点 附加...

    validnumberleetcode自动机-code_with_py:编程任务

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...

    validnumberleetcode自动机----:数据结构打卡

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...

    leetcode有效期-algorithmTask:算法任务

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 ...

    leetcode2sumc-DataWhale_exercise:用C++编程

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...

    leetcode有效期-Datawhale-Coding:Datawhale-编码

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...

    leetcode爬楼梯排列组合解法-algorithm_practice:对数据结构、算法相关的编程实践(DataStructureandAl

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!) 2、链表 实现单链表、循环链表、双向链表 支持增删操作 实现单链表反转 实现两个有序的链表合并为一个有序链表 ...

    lrucacheleetcode-DataStructure:数据结构

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 ...

    leetcode卡-Programming:编程

    学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 练习: Three Sum(求三数之和) Majority Element(求众数) Missi

Global site tag (gtag.js) - Google Analytics