202. Happy Number

Easy

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:

Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
package leetcode.easy;

public class HappyNumber {
public boolean isHappy(int n) {
if (n == 0) {
return false;
}
while (n != 1) {
int n2 = n;
n = 0;
while (n2 > 0) {
n += (n2 % 10) * (n2 % 10);
n2 /= 10;
}
if (n == 4) {
return false;
}
}
return true;
} @org.junit.Test
public void test() {
System.out.println(isHappy(19));
}
}

最新文章

  1. RecyclerView,CardView导入和使用(Demo)
  2. python 字符串比较
  3. 15套帮助你展示 App 设计的透视屏幕原型素材
  4. poppin_xpower_ 常城
  5. python分片
  6. PHP.8-HTML+CSS(二)-HTML详解
  7. C++单元测试2
  8. 前端开发的常用js库
  9. HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!
  10. Codeforces 803C. Maximal GCD
  11. H5外包 微信小程序外包 小程序外包 就找北京动点开发团队
  12. linux使用.net core 创建简单的MVC
  13. 记录一下putty的pscp的用法【转】
  14. jquery $.trim()去除字符串空格
  15. 自建k8s集群日志采集到阿里云日志服务
  16. linux驱动(续)
  17. Salesforce小知识:在简档中设置Visualforce页面的权限
  18. 闭包传递(floyed)
  19. sqlserver无法在数据库上放置锁
  20. Python网络爬虫之图片懒加载技术、selenium和PhantomJS

热门文章

  1. kafka的HA机制
  2. learning java 字符串常用操作
  3. saltstack 发布 iis 站点
  4. mysql创建账号、授权、数据导出、导入
  5. P5024 保卫王国(动态dp/整体dp/倍增dp)
  6. ZR#1009
  7. python骚操作之内建方法的使用
  8. 范仁义html+css课程---1、html基本结构
  9. idea 启动web项目
  10. 微信小程序网络通信(一)