package isHappy202;
/*
* 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.
*/ public class Solution {
/*
* solution:
* 1
* 2-4-16-37-58-89-145-42-20-4
* 3-9-81-65-61
* 4
* 5-25-29-85-89
* 6-36-45-41-17-50
* 7-49-97-130-10
* 8-64-52-29
* 9-81-65
* so only 1 and 7 is "happy"
*/
public static boolean isHappy(int n) {
while(n/10>0){
int sum=0;
while(n/10>0){
sum+=Math.pow((n%10),2);
n=n/10;
}
sum+=Math.pow(n,2);
n=sum;
}
if (n==1||n==7)
return true;
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(isHappy(23456));
}
}

LeetCode----202. Happy Number(Java)的更多相关文章

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

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

  2. Java for LeetCode 202 Happy Number

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

  3. Java [Leetcode 202]Happy Number

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

  4. LeetCode 202. Happy Number (快乐数字)

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

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

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

  6. LeetCode 202 Happy Number

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

  7. (easy)LeetCode 202.Happy Number

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

  8. 40. leetcode 202. Happy Number

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

  9. LeetCode 5108. Encode Number - Java - 2进制

    题目链接:https://leetcode-cn.com/problems/encode-number/ Given a non-negative integer num, Return its en ...

  10. leetcode 136. Single Number ----- java

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

随机推荐

  1. java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序

    import java.util.ArrayList;import java.util.Collections;import java.util.List; public class Test7{   ...

  2. thinkphp关联模型的用法

    HAS_ONE(值得注意的是,这是主动关联,外键必须是被关联的表): <?php namespace Home\Model; use Think\Model\RelationModel; cla ...

  3. HttpSessionListener和HttpSessionBindingListener监听session的销毁

    1. 使用HttpSessionListener public class OnlineUserListener implements HttpSessionListener { public voi ...

  4. 日历js插件

    因为做了一个培训管理模块,要有一个开始与结束培训时间.时间日期如果个用户手动输入的话,即使你要求了时间格式,但是用户可能还是会输错时间格式.所以想想,还是找了一个js日历插件.下面来介绍下我自己用的一 ...

  5. Emgu 决策树

    MCvDTreeParams cvFolds //If this parameter is >1, the tree is pruned using cv_folds-fold cross va ...

  6. 【iCore3 双核心板_FPGA】实验二十:基于FIFO的ARM+FPGA数据存取实验

    实验指导书及代码包下载: http://pan.baidu.com/s/1cmisnO iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...

  7. 【7集iCore3基础视频】7-5 iTool2驱动安装

    iTool2驱动安装: 高清源视频:链接:http://pan.baidu.com/s/1dF5FtlB%20密码:g5x7 iCore3 购买链接:https://item.taobao.com/i ...

  8. [daily][archlinux][fonts] 在linux下管理字体

    序: linux是社区搞出来, 商业应用也都是服务器场景.社区里又都是技术人员.字体又是细节.而且会英文早成了标配.所以没有很多社区以外的人力来搞字体这个毫无回报的东西. 结果很自然的,装linux桌 ...

  9. UIView常见属性设置汇总

    1.圆角设置 viewT.layer.cornerRadius = 10;//设置那个圆角的有多圆 viewT.layer.borderWidth = 10;//设置边框的宽度,当然可以不要 view ...

  10. MVC过滤器详解 面向切面编程(AOP)

    面向切面编程:Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个 ...