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 + 9= 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

happy number是把一个数的各个位置的数字的平方相加,直到值为1为止。如果一个数是happy number,那么最终的结果一定会是1,如果不是,这个过程将一直持续下去,并且会重复出现以前曾经出现过的数。例如2。

22 = 4

42 = 16

12 + 62 = 37

32 + 72 = 58

52 + 82 = 89

82 + 92 = 145

12 + 42 + 52 = 42

42 + 22 = 20

22 + 02 = 4

···

所以2不是一个happy number。我们的思路就是记录这个过程中出现过的每一个数,如果某些数字在过程中重复出现,则该数肯定不是happy number。如果最后结果是1,则该数是happy number。代码如下:

 public class Solution {
public boolean isHappy(int n) {
List<Integer> list = new ArrayList<>();
while (n != 1) {
if (list.contains(n)) {
return false;
}
list.add(n);
n = toArrayAndSum(n);
}
return true;
} public static int toArrayAndSum(int n) {
int sum = 0;
while (n > 0) {
int x = n % 10;
n = n / 10;
sum = sum + x * x;
}
return sum;
}
}

LeetCode OJ 202. Happy Number的更多相关文章

  1. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  2. 【LEETCODE OJ】Single Number

    Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...

  3. 【一天一道LeetCode】#202. Happy Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  4. 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】202 - Happy Number

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

  6. LeetCode OJ 之 Ugly Number II (丑数-二)

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  7. LeetCode OJ:Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  8. LeetCode OJ:Ugly Number II(丑数II)

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  9. LeetCode OJ:Happy Number(欢乐数)

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

随机推荐

  1. Spring MVC(一)

    MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域,MVC特指一种表现层设计模式,不限于Java语言 spring mvc属于spring框架的后续产品,用在基于MVC的表现层开发,类 ...

  2. Toast用法

    应用场景:弹出提示信息 主界面: 代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sa ...

  3. TCP三次握手中,为什么需要第三次握手?

    为什么客户端A还要发送一次确认呢?(为什么需要第三次握手) 这主要是为了防止已失效的连接请求报文段突然又传送到了B(服务器端),因而产生错误. 所谓"已失效的连接请求报文段"是这样 ...

  4. FR javascript 时间设置上个月最后一天后当月最后一天

    //设置上月最后一天 var date1 =new Date(); date1.setDate(1); //first day; date1.setMonth(date1.getMonth()); / ...

  5. Linux下Crontab的格式及含义

    crontab的基本格式: f1 f2 f3 f4 f5 command 分 时     日 月 周 命令 第一列f1代表分钟1~59:当f1为*表示每分钟都要执行:为*/n表示每n分钟执行一次:为a ...

  6. android项目实战 --ListView 头部ViewPager广告轮询图效果

    看开源框架:https://github.com/tianshaojie/AndroidFine,里面有如下效果,特记录学习下,以后项目中用也好能够立刻想起来.   如上面所示,是常见项目中的图片轮训 ...

  7. C#中的??是什么意思

    C#中的??是什么意思 DJ8Angus | 浏览 49982 次  2012-01-16 12:07 2012-01-16 12:23   最佳答案   如果不赋予初值,C#的变量是不允许直接使用的 ...

  8. noip2013Day2T3-华容道【一个蒟蒻的详细题解】

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  9. 第八十四节,css布局小技巧及font-awesome图标使用

    css布局小技巧及font-awesome图标使用 图片鼠标放上去遮罩效果,显示文字 当鼠标放上去时 /*最外层div*/ .a{ width: 384px; height: 240px; backg ...

  10. 《JS权威指南学习总结--8.8.4 记忆函数》

    内容要点:   可以将上次的计算结果缓存起来.在函数式编程当中,这中缓存技巧叫做 "记忆". 需要注意的是,记忆只是一种编程技巧,本质上是牺牲算法的空间复杂度以换取更优的时间复杂度 ...