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

  • 1 + 81 = 82
  • 64 + 4 = 68
  • 36 + 64 = 100
  • 1 + 0 + 0 = 1

欢乐数,就是不断将位的平方相加为新数,看看最后得到的数是不是1,是的话就是欢乐数,否则不是。代码比较简单,用一个set保存下来已经求到过的不是1的数字,一旦

后期某个计算到的数字能在set中找到(不是1),那么就一定进入到死循环当中了。这是就应该返回false, 否则一旦某个计算结果是1那么久返回true,代码见下:

 class Solution {
public:
bool isHappy(int n) {
set<int> sample;
sample.insert(n);
int s = ;
for(;;){
while(n != ){
s += (n % ) * (n % );
n /= ;
}
if(s == )
return true;
else{
if(sample.find(s) != sample.end())
return false;
sample.insert(s);
n = s;
s = ;
}
}
}
};

java版本如下所示,方法相同:

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

LeetCode OJ:Happy Number(欢乐数)的更多相关文章

  1. [LeetCode] 136. Single Number 单独数

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  2. [LeetCode] 246. Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  4. [LeetCode] 263. Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

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

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

  6. LeetCode OJ -Happy Number

    题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...

  7. [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...

  8. LeetCode OJ 之 Number of Digit One (数字1的个数)

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  9. LeetCode OJ:Number of Islands(孤岛计数)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  10. LeetCode OJ:Number of 1 Bits(比特1的位数)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

随机推荐

  1. 实现num1、num2交换,无中间变量

    num1=num1+num2; num2=num1-num2; num1=num1-num2;

  2. 认识与学习BASH(下)

    1.设定数组的变量与内容:var[index]=content,数组的读取:建议直接以$(数组)的方式来读取 例:echo“${var[1]},${var[2]},${var[3]}” 2.变量内容的 ...

  3. 主机名 域名 网站名 URL

    举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...

  4. 转:用unix socket加速php-fpm、mysql、redis的连接

    图虫的服务器长期是单机运行.估计除了mysql之外,php-fpm和redis还可以在单机上共存很长时间.(多说服务器早就达成了单机每日2000万+动态请求,所以我对单机搞定图虫的大流量非常乐观) 如 ...

  5. MySQL -表完整性约束(Day41)

    阅读目录 一.介绍 二.not null 与 default 三.unique 四.primary key 五.auto_increment 六.foreign key 七. 总结 一 介绍 回到顶部 ...

  6. numpy.linspace介绍

    numpy.linspace:在指定范围内返回均匀间隔的数组 In [12]: import numpy as np In [13]: result = np.linspace(1,10) #默认生成 ...

  7. Python(面向对象3 ——实例)

    这个小东西包括了最近学习的,包括模块.包.序列化.继承.派生.组合的应用举例.整体架构如图: bin是程序入口,core包括了几个主要逻辑,main是主架构,login包括登录功能,register包 ...

  8. flask-基础知识

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  9. seven habits of highly effective people 高效能人士的七个习惯

    习惯的模型 : dependent 依赖  -- independent 独立自主 --interdependent  互相依赖 1: be  proactive 主动积极 what you can ...

  10. maven Eclipse实战材料整理

    最近在看github上面的项目,发现好多的源码都是maven组织的,但又要去使用maven,因此找资料学习,但是效果很不好,直到昨天晚上看了mooc上面的视频,理清了自己的思路,特将资料列表如下: 视 ...