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

题目标签:Hash Table

  题目给了我们一个数字,让我们判断它是不是 Happy Number。

  Happy Number 最终会变成1,很好判断;Non-Happy Number 最后会无限循环在一个cycle。

  所以,我们要解决如何判断 一个数字是否在一个无限循环里,可以建立HashSet 记录它每一次的变化数字,一旦它重复了,说明,它在循环。

Java Solution:

Runtime beats 61.14%

完成日期:05/16/2017

关键词:HashSet

关键点:利用HashSet 记录每一个数字

 class Solution
{
public boolean isHappy(int n)
{
HashSet<Integer> set = new HashSet<>(); while(n != 1) // happy number will get out of this loop
{
if(set.contains(n)) // if a number repeats in a cycle, return false;
return false; set.add(n); int sum = 0; while(n != 0) // get each digit from number
{
int digit = n % 10; sum += digit * digit;
n = n / 10;
} n = sum; // update n with new number
} return true;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 202. Happy Number (快乐数字)的更多相关文章

  1. [LeetCode] 202. Happy Number 快乐数

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

  2. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  3. 202 Happy Number 快乐数

    写一个算法来判断一个数是不是“快乐数”.一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,或是无限循环但始终变不到 1.如 ...

  4. [LeetCode] 507. Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  5. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  6. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  7. Java for LeetCode 202 Happy Number

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

  8. (easy)LeetCode 202.Happy Number

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

  9. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

随机推荐

  1. 在控制台显示“Hello World”

    //作者:江骆 //功能:在控制台显示"Hello World" //日期:2017.7.19 public class helloworld   //public 表示公共类,一 ...

  2. python读取外部文件

    >>> pd.read_excel('c://111.xlsx') 年度排名 历史排名 电影名称 总票房 总人次 总场次 上映年份 操作 0 1 1 美人鱼 NaN -- -- 20 ...

  3. Spring - bean的lazy-init属性(懒加载)

    默认情况下,容器初始化的时候便会把bean实例化,通常这样做可以让一些配置或者bean实例化的异常在容器启动的时候就发现,而不是在N久之后.但有时候,我们希望某个可能不会用到但又不是100%不用的be ...

  4. 常量池之String.intern()方法

    JDK7将String常量池从Perm区移动到了Java Heap区.在JDK1.6中,intern方法会把首次遇到的字符串实例复制到永久代中,返回的也是永久代中的实例.但是在JDK1.7以后,Str ...

  5. 记一次Linux下给硬盘分区格式化操作

    今天找到一张旧TF卡,2G的,正好拿来练习下建立分区 插上orangepi后,fdisk -l看看,可以看到多了一个新的存储设备 /dev/mmcblk1 用fdisk打开它: fdisk /dev/ ...

  6. Eclipse 多行复制并且移动失效

    Eclipse  有个好用的快捷键  即 多行复制 并且移动 但是 用 Win7 的 电脑 的 时候 发现屏幕 在 旋转 解决方案: 打开Intel的显卡控制中心 把旋转  的 快捷键 进行更改 就好 ...

  7. MX4拍摄视频转码方法

    问题 使用魅族4手机拍摄的视频,其视频编码是H.265 目前大多数设备不支持解码,表现为常用播放器无法正常播放视频,剪辑软件无法剪辑视频. 解决方案 使用软件进行转码,期间尝试软件如下: 爱剪辑 部分 ...

  8. 关于web前端代码艺术

    以前一直都以为html代码要分离得很好,html一个文件,css一个文件,js一个文件,然后最好一个html页面里面不要要太多冗余的代码,不要恶心地引入一个又一个的js,连jquery的引入我都觉得有 ...

  9. 组合 Lucas定理

    组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Ba ...

  10. ch6-条件渲染(v-if v-else v-else-if key管理可复用元素 v-show )

    1 v-if 1.1 简单使用 <h1 class="h1" v-if="ok">yes</h1> <script> var ...