+ 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. Google图片加载库Glide的简单封装GlideUtils

      Google图片加载库Glide的简单封装GlideUtils 因为项目里用的Glide的地方比较多,所有简单的封装了以下,其实也没什么,就是写了个工具类,但是还是要把基础说下 Glide的Githu ...

    2. Python Generator 运行细节验证

      今天来__next__和send, 改天来throw和close class A: def __setattr__(self, key, val): print('set %s to %s'%(key ...

    3. Hive-ORC文件存储格式(续)

      本文在Hive-ORC文件存储格式的理论基础上,进一步分析一个实际的Hive ORC表中的数据存储形式. 一.表结构 库名+表名:fileformat.test_orc 字段 类型 category_ ...

    4. 1.httpClient和ScrollView

      1 在服务器端使用sqllite编写数据库 常见命令是:sqlite3 tank.db 进入之后创建表: create table tscore ( id integer primary key au ...

    5. EBS开发常用编译命令

      一.编译FORM 1.将脚本写成shell脚本 cd $AU_TOP/forms/ZHS export FORMS_PATH=.:$FORMS_PATH:$AU_TOP/forms/ZHS frmcm ...

    6. 【NPR】漫谈轮廓线的渲染

      写在前面 好久没写文章.最近在看<Real Time Rendering, third edition>这本书,看到了NPR这一章就想顺便记录下一些常见的轮廓线渲染的方法. 在非真实感渲染 ...

    7. 15 Actionbar的显示和隐藏

      ActionBar 显示隐藏方法: 在布局文件中设置 theme主题 <?xml version="1.0" encoding="utf-8"?> ...

    8. 【ShaderToy】基础篇之再谈抗锯齿(antialiasing,AA)

      写在前面 在之前的基础篇中,我们讲到了在绘制点线时如何处理边缘的锯齿,也就是使用smoothstep函数.而模糊参数是一些定值,或者是跟屏幕分辨率相关的数值,例如分辨率宽度的5%等等.但这种方法其实是 ...

    9. 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

       1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...

    10. AndroidVerifyBoot

      253        Utils.write(image_with_metadata, outPath);254    }227行得到boot.img的size 238行new一个BootSignat ...