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. sed命令基础2

    我在sed命令基础里面说了一下sed的基础用法,sed还有一些高级用法,由于我也是在学习中,写的博客可能会有想不到的地方,有问题希望大家指出. sed的高级用法主要在于两个空间的使用,模式空间和保持空 ...

  2. POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)

    Problem Description In this problem, you have to analyze a particular sorting algorithm. The algorit ...

  3. 用reduce实现阶乘计算

    def fact(a,b): return a*b from functools import reduce print(reduce(fact,range(1,6))) from functools ...

  4. Kafka水位(high watermark)与leader epoch的讨论

    ~~~这是一篇有点长的文章,希望不会令你昏昏欲睡~~~ 本文主要讨论0.11版本之前Kafka的副本备份机制的设计问题以及0.11是如何解决的.简单来说,0.11之前副本备份机制主要依赖水位(或水印) ...

  5. 关于IOS的屏幕适配(iPhone)——Auto Layout和Size Classes

    Auto Layout和Size Classes搭配使用极大的方便了开发者,具体如何使用Auto Layout和Size Classes大家可以参考其他文章或者书籍,这里只提一点,在我们设置Size ...

  6. oracle 数据字典和动态性能视图

    一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...

  7. Win7 32位系统下Sublime text 3的安装以及配置C/C++、java、python的开发环境方法

    本人初学者,此文仅是对这几天鼓捣subime text 3一点微不足道的经验总结,如有明显错误,欢迎指正! 好了,废话少说,进入正题,之前编程java一直用的是eclipse,java的主流IDE,后 ...

  8. bzoj4403(模板题)

    序列统计,将答案转化,然后就是Lucas的模板题,用费马小定理瞎搞. #include<cstdio> #include<iostream> #include<algor ...

  9. http://codeforces.com/contest/349

    A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  10. node-Telnet

    什么是Telnet(window系统) 使用Telnet工具作为客户端对创建的TCP服务器进行会话交流时遇到的问题做一个简单的记录.希望可以帮助到一些同学. 这里看一下百度词条的解释 Telnet协议 ...