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));
}
}

LeetCode_202. Happy Number的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. Codeforces Round #609 (Div. 2) 【A,B,C】

    题意:给一个n<=1e7,找两个合数a和b使得a-b的差为n. 构造a=3n,b=2n,必含有公因子n,只有当n是1的时候是特例. #include<bits/stdc++.h> u ...

  2. jsp页面解析出错,或出现下载情况.

    当没有下面代码时,会出现空白页的bug <dependency> <groupId>org.apache.tomcat.embed</groupId> <ar ...

  3. RookeyFrame 添加 导入功能

    我遇到的情况: 我在‘模块管理’里面, 把模块‘客户联系人’的‘允许导入’勾上,保存后,在‘客户联系人’页面没有导入按钮, 模块‘客户主数据’却可以, 这是为什么额?两个模块都是线下创建的,是哪儿不一 ...

  4. C语言指针的使用例子(1)指针地址的输出

    #include <stdio.h> int main(void) { int a=10; int *p = &a; *p = 89; printf("变量值a=%d a ...

  5. 二维$MLE$线段树

    关于二维线段树,ta死了 先来看看两种二维线段树的打法 1.四叉树 然而ta死了,ta是$\Theta (n)$的,加上线段树的常数,$T$飞稳 2.线段树套线段树 我尽量画出来... 图中每个方块是 ...

  6. 公司和家里代码文件同步方案: (git和dropbox实现)

    公司和家里代码文件同步方案: (git和dropbox实现) 参与公司福利购入了有补贴的macbook pro后, 就不用上下班背着电脑了. 但是也出现了另外一问题: 家里和公司代码同步的问题 公司有 ...

  7. JSP了解点基础

    了解即可: 1.JSP本质: 是将jsp文件解析为java servlet类! 生成.class文件   存放在工程的work文件夹内! 2.注释  <%--   --%>    html ...

  8. java非空判断

    是否为 null 是否为 "" 是否为空字符串(引号中间有空格)  如: "     ". 制表符.换行符.换页符和回车 一. 字符串 1. if(str == ...

  9. redis主从复制读写分离

    主从复制,读写分离 Master/Slave 是什么 master写入 slave读取 能干嘛 读写分离,更加安全,性能提升 怎么玩 一主二仆.薪火相传.反客为主 周明老师,能够把长篇大论总结的很精辟 ...

  10. ES6——class类继承(读书笔记)

    前言 我一定是一个傻子,昨天这篇文章其实我已经写好了一半了,但是我没有保存 这是学习ES6的过程,我没有系统的看完阮大大的书.零零散散的,很多功能知道,但是没有实际的用过 看了几遍,总是看前面几章,所 ...