(easy)LeetCode 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
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
思路:使用HashSet判断重复。
代码如下:
public class Solution {
public boolean isHappy(int n) {
if(n<1) return false;
if(n==1) return true;
Set<Integer>set=new HashSet<Integer>();
set.add(n);
while(true){
int m=0;
int sum=0;
while(n!=0){
m=n%10;
n=n/10;
sum+=m*m;
}
if(sum==1) return true;
if(set.contains(sum)) return false;
set.add(sum);
n=sum;
}
}
}
运行结果:
(easy)LeetCode 202.Happy Number的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 263.Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【easy】202. Happy Number
happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...
随机推荐
- 创建对象的最好方式&最好的继承机制(代码实例)
/* 创建对象的最好方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ function People(sname){ this.nam ...
- mysql的text的类型注意
不要以为text就只有一种类型! Text也分为四种类型:TINYTEXT.TEXT.MEDIUMTEXT和LONGTEXT 其中 TINYTEXT 256 bytes TEXT 65,535 byt ...
- 前端js上传文件 到后端接收文件
下面是前端js代码: <html> <head> <meta http-equiv="Content-Type" content="text ...
- C语言数组初始化全部为0
] = {}; 编译器会把第一个初始化值(这里是0)赋给数组的第一个元素,然后用默认值0赋给其余的元素.如果没有给出初始值,编译器不会去做初始化工作.这样简洁的方式让代码更加高效. 另一种,就是mem ...
- [tomcat] tomcat+nginx 负载均衡配置
首先下载,安装tomcat. 修改tomcat端口,修改server.xml: 1.修改tomcat端口(默认8080) <Connector port="8383" pro ...
- java数组操作
@Bizlet("数据对象扩展运算逻辑")public class DataObjectExt { private DataObjectExt(){ //工具类不能实例化 } /* ...
- discuz!3 二次开发C#学者
PHP入门,从搞数据库开始: 大致看了以下PHP还是很简单的 比如链接数据库,就这么几行,比asp.net简单的多,就是需要自己搞数据的显示,需要精通HTML和代码生成技术: <?php $co ...
- bzoj3007: 拯救小云公主
Description 英雄又即将踏上拯救公主的道路…… 这次的拯救目标是——爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是 ...
- idea系列新版注册模式
http://idea.qinxi1992.cn/ 楼上被列入黑名单,用 http://114.215.133.70:41017/
- C语言每日一题之No.7
今天是正式第一天在现有的世界里与自己相处,你再也没有另一个世界可以躲避了.终于要自己面对自己了,一个人要真实的面对自己的灵魂总是痛苦的.从学校到社会的环境转换,现实与理想的冲突,个人价值观和社会价值观 ...