+ 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. Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》

      在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...

    2. 在Mac上搭建React Native开发环境

      概述 前面我们介绍过在window环境下开发React Native项目,今天说说怎么在mac上搭建一个RN的开发环境. 配置mac开发环境 基本环境安装 1.先安装Homebrew:用于安装Node ...

    3. ELK平台的搭建

      ELK是指Elasticsearch + Logstash + Kibaba三个组件的组合.本文讲解一个基于日志文件的ELK平台的搭建过程,有关ELK的原理以及更多其他信息,会在接下来的文章中继续研究 ...

    4. 在自己笔记本电脑上如何访问虚拟机的内容、包括可以使用ssh、访问tomcat、访问nginx

      1.给自己的电脑设置一个回环网卡,关于如何配置回环网卡,可以百度搜索一下 设置好后的状态如下: 并把回环网卡的ipv4的值设置成192.168.1.1 配置如下: 2.将vmware中的"虚 ...

    5. 删除表中重复行SQL

      delete from table_name a where rowid < (select max(rowid) from table_name b where a.col1 = b.col1 ...

    6. 【问题汇总】ScrollView嵌套GridView的问题

      在开发中遇到了ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当它们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法,自定义一个GridView控件. [ ...

    7. 【一天一道LeetCode】#257. Binary Tree Paths

      一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

    8. 05 Activity知识

      1.Activity          >概念:活动面板   应用程序组件  可以绘制Ui界面  可以和用户进行交互     默认展示全屏  其他情况 界面比其他窗口小  悬浮在其他窗口上方   ...

    9. SpringMVC系列之(一) 入门实例

      Spring MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也 ...

    10. pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)

      pig 脚本运行不需要后缀名 pig脚本名为tempfile,无后缀名 用pig -f tempfile 可直接运行 另外,pig tempfile也可以直接运行 这样就可以用python临时文件存储 ...