LeetCode OJ 202. 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为止。如果一个数是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的更多相关文章
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【一天一道LeetCode】#202. Happy Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】202 - Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode OJ 之 Ugly Number II (丑数-二)
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- LeetCode OJ:Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- LeetCode OJ:Ugly Number II(丑数II)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode OJ:Happy Number(欢乐数)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- webStorm(一)
1.打开webStorm选择activation code输入注册码 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIj ...
- Note_JavaWeb_Jars
- 【.NET】字符串处理
类名:DealString /// 1.截取字符串,最后加3个小数点 /// 2.获得指定Url的参数的string类型值 /// 3.判断数据类型 /// 4.过滤JS标记 /// 5.获取Chec ...
- ZZNU 1993: cots' friends
题目描述 cot 最近非常喜欢数字, 喜欢到了什么程度呢, 已经走火入魔了.... cot把每个朋友编上一个序号,然后遇到谁就叫"XX号",对此,他的朋友们一致认为cot" ...
- android 进程和线程管理
进程和线程的概念: 进程:程序的运行实例. 线程:cpu调度基本单位. Activity启动的时候,启动一个主线程,两个binder线程. 主线程实如何产生的?ZygoteInit启动,经由一系列调用 ...
- 第六十二节,html分组元素
html分组元素 学习要点: 1.分组元素总汇 2.分组元素解析 本章主要探讨HTML5中分组元素的用法.所谓分组,就是用来组织相关内容的HTML5元素,清晰有效的进行归类. ...
- 配置IIS提示打开目录浏览时的问题:未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的解决办法
错误消息: 未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的 ...
- CSS3秘笈复习:第十一章
1.text-align与vertical-align: text-align控制水平方向的定位,关键字是left.right.center和justify. vertical-align控制垂直方向 ...
- 遍历(一)jquery $().each和$.each()
原文:http://www.frontopen.com/1394.html 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法. $().each 在dom处理上 ...
- Storm常见问题处理
错误1:发布topologies到远程集群时,出现Nimbus host is not set异常.异常内容如下所示: [root@xop-dev-a bin]# ./storm jar /home/ ...