https://leetcode.com/problems/happy-number/

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: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

解题思路:

这题看似很复杂,其实不太难。唯一的难点,题目已经给出了,就是如果结局不是1的话,必然是一个loop。那么我们只要用一个set记录所有计算过的结果就行了。如果set重复了,并且没有1,就返回false。

public class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<Integer>();
int result = squaresDigits(n);
while(result != 1 && !set.contains(result)) {
set.add(result);
result = squaresDigits(result);
}
if(result == 1) {
return true;
}
return false;
} public int squaresDigits(int n) {
int result = 0;
while(n > 0) {
int lastDigit = n % 10;
result = result + lastDigit * lastDigit;
n = n / 10;
}
return result;
}
}

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 ...

  10. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

随机推荐

  1. Fedora 20 创建桌面快捷方式

    创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...

  2. Powerdesigner15 创建数据库生成脚本

    Ctrl + Shift + X,打开脚本编辑界面,使用VBScript编写脚本 点击帮助按钮,可以打开OLE Help,“Libraries >> PdPDM”中可以查看内置的类库.“A ...

  3. mac OS X下制定ll指令

    ll作为ls -l的快捷方式,但系统本身没有,需要通过如下方法生成 1.在用户目录下新建.bash_profile文件 # vim .bash_profile 2.添加内容 alias ll = 'l ...

  4. Spark菜鸟学习营Day2 分布式系统需求分析

    Spark菜鸟学习营Day2 分布式系统需求分析 本分析主要针对从原有代码向Spark的迁移.要注意的是Spark和传统开发有着截然不同的思考思路,所以我们需要首先对原有代码进行需求分析,形成改造思路 ...

  5. WPF以Clickonce方式发布后使用管理员身份运行

    WPF的程序,在发布时采用的Clickonce方式发布,Win7的用户安装完成之后,发现执行某些操作的时候会导致程序异常.在排查后发现,是权限问题导致.如图: 是执行File.Move时引发的异常:对 ...

  6. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  7. C#获取数据库中的Instance

    如果我现在要写个代码生成器,连接数据库,那你得知道有哪些Database存在吧,不然咋整? 在VS中我们添加一个ADO.NET的实体模型 在选择数据库名称的时候就是获取了数据库中Database In ...

  8. python之内置类型: 序列, 字典

    序列: 元素之类有序的类型. Python 2.x支持6种内置序列: list, tuple, string, ustring, buffer, xrange (1)序列的定义: list: [] t ...

  9. 关于qt5在win7下发布 & 打包

    QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...

  10. matlab实现高斯牛顿法、Levenberg–Marquardt方法

    高斯牛顿法: function [ x_ans ] = GaussNewton( xi, yi, ri) % input : x = the x vector of 3 points % y = th ...