题目描述:

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

解题思路:

用Set记录平方和的值,如果该值出现过,则表明进入新的循环,返回false;否则继续计算平方和,直到和为1为止。

代码如下:

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

  

Java [Leetcode 202]Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  6. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  8. Java [Leetcode 260]Single Number III

    题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  9. Java [Leetcode 263]Ugly Number

    题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

随机推荐

  1. iframe嵌入其他网站,如何自适应高度

    终于有一周时间,工作不那么忙了,腾出手来总结下工作过程中学到的知识. 每天遇到新问题,解决新问题,但是却很少有时间去仔细研究下,或者总结下.攒的多了,就得从头捋一遍. 说下iframe自适应高度: 搜 ...

  2. C# textbox 滚动条 随文本输入 滚动

    tb_Log.SelectionStart = tb_Log.Text.Length;//设置光标位置 tb_Log.ScrollToCaret();//随文本输入 滚动

  3. uva 10771

    思路题 K的人数只能以2减少 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  4. Unity3D IOS IPhone添加Admob的方法

    原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140403119293/ 首先阅读官方文档https://developers. ...

  5. iscsi 操作备忘

    #查找iscsi节点 iscsiadm -m discovery -t st -p 192.168.20.225 #列出可用节点 iscsiadm -m node -T iqn.com.exapmle ...

  6. [转载]实战Linux下VMware虚拟机根目录空间扩充

    [转载]实战Linux下VMware虚拟机根目录空间扩充 (2011-07-31 21:34:34) 转载▼ 标签: 转载   原文地址:实战Linux下VMware虚拟机根目录空间扩充作者:shar ...

  7. 恢复mdf文件到数据库方法

    CREATE DATABASE crm_testdb1 ON (FILENAME = N'C:\e527051\crm_testdb\crm_testdb_20121104.mdf')FOR ATTA ...

  8. LeeCode 1-Two Sum

    Two Sum Total Accepted: 125096 Total Submissions: 705262 Question Solution Given an array of integer ...

  9. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...

  10. 利用纯真ip地址库 查询 ip所属地

    1. 首先下周数据源,选择是纯真数据库:http://www.cz88.net/ 2. 安装后,打开软件,将数据导出为txt格式. 3. 处理数据,参照网上的文章(http://www.jb51.ne ...