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. c字符数组之两头堵模型

    char *其实就是char[length]的首元素地址  实验环境:centos7下qt5.11 中文char类型占3个字节 char[length]="特别车队"其实等价于ch ...

  2. Kafka 基础操作

    cd /root/kafka/kafka_2.10-0.8.2.2/bin 1.查看kafka topic kafka-topics.sh --list --zookeeper 172.16.100. ...

  3. 七.搭建基本的FTP服务

    1.安装vsftpd软件包 ]# yum -y install vsftpd 2.重起vsftpd服务 ]# systemctl restart vsftpd ]# systemctl enable ...

  4. 2019/7/18 --1.<%@ include file=""%>与<jsp:include page=""/>两种方式的作用

    一.前言 身为一名coder有太多太多的知识点要去学,太多太多的东西要去记.往往一些小细节也就难免疏忽,但悲催的是多数困恼你的bug就是因为这些微不足道的知识点.我们又不是机器人,怎么可能什么都记得了 ...

  5. redis堵死致数据清空

    情景: zy的链路监控突然都恢复,而且在哪个时间段zabbix中显示回复,也发送了告警,但是实际上告警并没有发出来.这是不可能的情况,应该是redis缓存中的数据都被清空了,没有认为干预,需解决问题 ...

  6. 关于nginx反代jenkins报错 反向代理设置有误

    官方文档地址: https://wiki.jenkins.io/display/JENKINS/Running+Jenkins+behind+Nginx 直接解决的配置文件吧. 这是使用子域名,不使用 ...

  7. Java ArrayList对象集合去重

    import java.util.ArrayList; import java.util.Iterator; public class StringSampleDemo { public static ...

  8. python 设计模式学习代码记录

    @工厂模式class Beijing: def printreslut(self): print("ok") class Shanghai: def printreslut(sel ...

  9. Jar hell问题以及解放方法

    当一个类或一个资源文件存在多个jar中,就好存在jar hell问题 可以通过以下代码来诊断问题:

  10. 使用Git GUI,上传项目到github,并实现预览功能

    一.使用GUI,上传项目到GitHub (GUI是啥,不做过多赘述,可百度了解) 步骤: 1.打开GUI,新建一个仓库,demo 2.在编辑器中,编写相关代码,比如添加1.html文件,文件内容为“h ...