+ 92 = 82

  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1
  • 思路:

    (1)题意为判断给定的整数是否为一个“快乐的数”,所谓快乐的数需要满足一下几个条件:将该整数的每个位上的数字的平方相加得到一个新的整数,循环对新的整数进行上述操作,如果最后所得整数收敛于1,则这样的数字为一个“快乐的数”。

    (2)首先,判断0肯定不是一个“快乐的数”;其次,对初始数字的每个位上数的平方相加,循环进行前面的操作;需要注意的是,对于可能会出现死循环的数字需要进行判断,需要判断循环的次数,这里通过测试得到循环次数为4,即如果4次循环过程中,每一次所得数字中都不收敛于1,则判断该数字不是一个“快乐的数”,否则,则是一个“快乐的数”;最后,对于那些循环得到的结果为1的数字,直接返回true,即该数字是一个“快乐的数”。

    (3)详情见下方代码。希望本文对你有所帮助。

    算法代码实现如下:

    /**
     * @author liqq
     */
    public class HappyNumber {
    	public static boolean isHappy(int n) {
    		if (n == 0)
    			return false;
    		return testhappy(0, n);
    	}
    
    	private static boolean testhappy(int number, int n) {
    		int sum = n;
    		int add = 0;
    		boolean hasone = false;
    		while (sum != 0) {
    			int square = (sum % 10) * (sum % 10);
    			add += square;
    			sum = sum / 10;
    			if (sum % 10 == 1) {
    				hasone = true;
    			}
    		}
    		number++;
    
    		int test = add;
    		while (test != 0) {
    			if (test % 10 == 1) {
    				hasone = true;
    			}
    			test = test / 10;
    		}
    
    		if (hasone == false && number > 4)
    			return false;
    
    		if (add != 1 || add % 10 != 1) {
    			boolean istrue = testhappy(number, add);
    			if (istrue)
    				return true;
    		} else {
    			return true;
    		}
    		return false;
    	}
    }

    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. 搭建ejabberd集群

      搭建ejabberd集群(金庆的专栏 2016.8)以2台机器搭建一个ejabberd集群.2台机器都是外网一块网卡,内网另一块网卡.新建一个域名,添加2台机器的外网IP.分别用源码安装ejabber ...

    2. Python 继承标准类时发生了什么

      定义标准类dict的一个子类c: >>> class c(dict): pass >>> y=c({1:2,3:4}) >>> y {1: 2, ...

    3. 重写方法的利器-super

      重写方法的利器-super class ilist(list): def __init__(self,dft=None,r=list()): super(ilist, self).__init__(r ...

    4. 24 AIDL案例

      服务端 MainActivity.java package com.qf.day24_aidl_wordserver; import android.app.Activity; import andr ...

    5. linux 最大文件描述符

      Linux对应用程序能打开的的最大文件描述符数量有两个层次的限制:用户级限制和系统级限制. 用户级限制是指目标用户运行的所有进程总共能打开的文件描述符数. 系统级的限制是指所有用户总共能打开的文件描述 ...

    6. cassandra 监控方案评估

      摘要 最开始做cassandra monitor 方案的选型时,主要是从cassandra 本身入手,后来发现cassandra运行在JVM上,所有的metrics都是通过JMX 暴露出来.所以又可以 ...

    7. 18 UI美化之level(等级显示显示)

      根据level显示哪张图片 在工程文件的res/drawable/新建level-list 如下 <?xml version="1.0" encoding="utf ...

    8. [virtualenv]生产环境中使用virtualenv

      virtualenv 对于python开发和部署都是好工具,可以隔离多个python版本和第三方库的版本,这里作者总结了几个常用python服务怎么样结合virtual部署 原文链接 Python 中 ...

    9. Android简易实战教程--第十二话《代码获取手机总运行内存的大小》

      手机RAM存储,类似于电脑的内存.这一篇,对通过代码获取手机总内存大小做详细介绍. 首先,定义一个engine类,这个类功能就是获取进程信息,包括运行的程序个数,系统总内存,系统剩余总内存.本篇先完成 ...

    10. TortoiseSVN文件夹图标不显示

      伴随着十二月的脚步,小编带领的市委组织部项目有条不紊的进行着,在最近的项目中遇到一个问题TortoiseSVN文件夹的图标不显示,为什么小编已经安装好TortoiseSVN了,发现文件夹的图标还是系统 ...