+ 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. 【SSH系列】一步步深入springmvc+商品列表查询demo

      在前面的博文中,小编主要简单的介绍springmvc的体系结构.mvc模式的优缺点以及mvc框架,今天我们来继续学习springmvc的相关知识,在这篇博文中,小编讲解过springmvc的体系结构, ...

    2. 剑指Offer——网易笔试题+知识点总结

      剑指Offer--网易笔试题+知识点总结 Fibonacci package cn.edu.ujn.nk; import java.util.ArrayList; import java.util.S ...

    3. 关于ListView中包含EditText数据复用引起异常的解决方案

      概述 前几天测试提了一个bug,在ListView中添加留言信息,导致错乱的问题.实际上就是ListView需要添加一个EditText,复用导致错乱的问题,这个问题以前也遇到过.诸如,ListVie ...

    4. ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse

      ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse 书中,大部分出现hydro的地方,直接替换为indigo或ja ...

    5. Swift中使用NSLog的问题

      在Swift中如果想要以如下方式执行NSLog,则编译器必定抱怨: class Foo {} NSLog("%@",Foo()) 因为Foo是原生Swift类不是继承自NSObje ...

    6. windows下Eclipse操作MapReduce例子报错:Failed to set permissions of path: \tmp\hadoop-Jerome\mapred\staging\

      windows下Eclipse操作MapReduce例子报错: 14/05/18 22:05:29 WARN util.NativeCodeLoader: Unable to load native- ...

    7. 使用Geolocation校正GDAL不支持的数据

      对于低分数据来说,常用的校正方式就是给定数据的经纬度查找表来进行校正.在GDAL中,这种校正方式叫Geolocation array.常用的数据有国外的MODIS数据,国内的如风云系列(FY)和海洋系 ...

    8. linux2.6内核链表

      一.        链表数据结构简介      链表是一种常用的组织有序数据的数据结构,它通过指针将一系列数据节点连接成一条数据链,是线性表的一种重要实现方式.相对于数组,链表具有更好的动态性,建立链 ...

    9. python使用qq服务器发送邮件

      python使用qq服务器发送邮件 直接上代码: #!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # sendmail.py # # init created: ...

    10. 08 ListView 优化的例子

      package com.fmy.homework; import java.util.List; import com.fmy.homework.httputil.HttpUtil; import c ...