题目描述:

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. Matlab实现求a到b被c整除的个数

    我先想到的是for循环........ 然后sum(find(mod(a:b,c)==0)),从10到100得到874,为什么不对呢? 比如a = [1 2 3 4  2 3 4 2],find(a= ...

  2. 解Linux进程间通信(IPC)方式

    http://blog.csdn.net/liuhongxiangm/article/details/7928790 linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对U ...

  3. 01-08-05【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate二级缓存:第三方MemCache缓存

    一.准备工作 [1]根据操作系统(位数)选择下载相应版本的MemCache, MemCache的下载和安装,参看: http://www.cnblogs.com/easy5weikai/p/37606 ...

  4. 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子

    原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...

  5. MongoDB 性能优化五个简单步骤

    MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...

  6. JAVA WEB中如何让数据库连接对开发人员完全透明?

    书上的技术,确实开了眼界. 列相关测试代码如下,慢慢体会开发的模式. PropsUtil package org.smart4j.chapter2.util; import java.io.FileN ...

  7. linux入门教程(五) Linux系统的远程登录

    首先要说一下,该部分内容对于linux初学者来讲并不是特别重要的,可以先跳过该章节,先学下一章,等学完后再回来看这一章. Linux大多应用于服务器,而服务器不可能像PC一样放在办公室,它们是放在ID ...

  8. Protege A DOT error has occurred错误

    问题参生的原因:graphviz没有安装或者,没有配置好 解决方法: 1.下载graphviz,这里是百度软件下载的,在官网下载需要注册账户,麻烦 2.安装graphviz,找到下面的路径. 3.设置 ...

  9. 【Linux高频命令专题(3)】uniq

    简述 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] [ -Fields ] [ +Characte ...

  10. springmvc文件上传2中方法

    基于前面文章的基础上. 一.准备 需要的jar  二.配置 1.  spmvc-servlet.xml <?xml version="1.0" encoding=" ...