题目描述:

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. 【BZOJ】【1272】【BeiJingWC2008】Gate of Babylon

    组合数学+容斥原理 Orz zyf-zyf 多重集组合数0.0还带个数限制?  ——>  <组合数学>第6章  6.2带重复的组合 组合数还要模P 0.0? ——> Lucas ...

  2. Java学习第二篇:类,对象,成员属性,成员方法,构造方法,类变量,类方法

    一.类的定义 一个全面的类定义是比较复杂的,  定义如下:

  3. Unity3D脚本中文系列教程(十)

    http://dong2008hong.blog.163.com/blog/static/4696882720140312627682/?suggestedreading&wumii Unit ...

  4. C# 中使用JSON - DataContractJsonSerializer

    C#中使用JSON不需要使用第三方库,使用.NET Framwork3.5自带的System.Runtime.Serialization.Json即可很好的完成JSON的解析. 关于JSON的入门介绍 ...

  5. Boost简介

    原文链接:  吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...

  6. 致诸位新程序员:来自Chuck Jazdzewski慈父般的忠告

    记住这几句话,学无止境.(Never stop learning.)沟通至关重要.(Communication is critical.)履行承诺,胜过交付.(Under promise, over ...

  7. VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)

    原文地址: VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)-软件开发-鸡啄米 http://www.jizhuomi.com/software/139.html   上一讲中鸡 ...

  8. iOS 精确定时器

    Do I need a high precision timer? Don't use a high precision timer unless you really need it. They c ...

  9. Android Handler与多线程

    本文首先解释一下handler是用来干嘛的,然后通过例子介绍其在多线程中的应用. 什么是Handler handler通俗一点讲就是用来在各个进程之间发送数据的处理对象.在任何进程中,只要获得了另一个 ...

  10. android从应用到驱动之—camera(2)---cameraHAL的实现

    本文是camera系列博客,上一篇是: android从应用到驱动之-camera(1)---程序调用流程 本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这 ...