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

LeetCode新题,题目大意:给定一个数,将其每一位平方和相加得到一个新的数,以此循环往复,最后得到的数如果是1,则给定的数是happy number,否则会无尽的循环。

解题思路:一开始直接模拟,循环100次还不是1直接剪掉return false,也能AC,但是毕竟可能有情况覆盖不到,改用HastSet存放中间数就行了,如果得到的新结果在中间数集合中出现了,那么一定会陷入循环并且得到的不是1。

Talk is cheap>>

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

Happy Number——LeetCode的更多相关文章

  1. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  2. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

  3. Happy Number - LeetCode

    examination questions Write an algorithm to determine if a number is "happy". A happy numb ...

  4. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. Largest Number——LeetCode

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. Letter Combinations of a Phone Number——LeetCode

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. Ugly Number leetcode java

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

  8. Single Number leetcode java

    问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...

  9. Palindrome Number leetcode java

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

随机推荐

  1. 为什么选择我--ReactJS

    在页面中如何大面积操作DOM的话,性能肯定是一个很大的问题,然而聪明的ReactJS实现了Virtual DOM技术,这是他的亮点之一.将组件的DOM结构映射到这个Virtual DOM对象上,并且R ...

  2. Echarts使用随笔(1)-Echarts中markPoint的使用(静态、动态)-effect

    先看一段关于初始化Echart   js的使用  myChart = echarts.init(document.getElementById('mainChart'));         var o ...

  3. 1、大部分社交平台接口不支持https协议。

    参考文献来自:http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85 ...

  4. node http.request请求

    var http = require('http'); var querystring = require('querystring'); var path = '/cricket/getRecord ...

  5. ubuntn 虚拟机NAT 静态IP 网络配置

    在虚拟机安装ubuntu12.04自动获取IP 一切都没有问题 ssh连接也正常.关机重启后郁闷的发现网络已经不通了,于是开始了以下的摸索. 1.配置静态IP 网关: ip段: 命令: Vim /et ...

  6. 一句实现jquery导航栏

    /*mine 才疏学浅,望大神绕行 */ .current{ color: red; font-size: large; } $(function(){ //实现点击标题的css和.lev1下面的a标 ...

  7. Bootstrap_Javascript_弹出框

    HTML: <button type="button" class="btn btn-default" data-container="body ...

  8. phpexcel 一些基本的设置 (表格的基本属性)

    网址是:http://www.thinkphp.cn/code/1893.html

  9. QTP插入Output Value和插入CheckPoint,注意点

    1. 必须打开程序才能进行Output value和CheckPoint的插入. 2. 也有可能是对象获取不到,从新加载对象库. 提示如下图:

  10. mysql数据类型——时间类型

    四种日期格式: 每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值. YEAR         0000 YYYY ...