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. Linux学习笔记(4)磁盘分区(fdisk)、挂载与文件系统命令

    Linux学习笔记(4)磁盘分区(fdisk).挂载与文件系统命令 1.磁盘分区是怎么表示的? 1.1 对于IDE接口,第一主盘为hda,第1从盘为hdb,第1从盘的第1个分区为hdb1 1.2 对于 ...

  2. Linux运维工程师:30道面试题整理

    1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...

  3. 001-docker概述、架构、window安装、基本测试

    一.概述 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流 ...

  4. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  5. 关于shared pool的深入探讨(五)

    Oracle使用两种数据结构来进行shared pool的并发控制:lock 和 pin.Lock比pin具有更高的级别. Lock在handle上获得,在pin一个对象之前,必须首先获得该handl ...

  6. shell相关知识

    在bash shell中,$( )与` ` (反引号)都是用来做命令替换用(command substitution)的.所谓的命令替换与我们第五章学过的变量替换差不多,都是用来重组命令行:*完成引号 ...

  7. python全栈开发之OS模块的总结

    OS模块 1. os.name()      获取当前的系统 2.os.getcwd      #获取当前的工作目录 import os cwd=os.getcwd() # dir=os.listdi ...

  8. ArcEngine开发中“错误类型"****"未定义构造函数”

    from:http://blog.csdn.net/mengdong_zy/article/details/8990593 问题 在ArcEngine开发的时候,在编译时,发现出现这样的错误,出错的地 ...

  9. javascript Date对象 之 date初始化

    javascript Date对象 --> 日期初始化: 总结: 日期初始化的 方式: 1. new Date( yyyy, M(+), d(+), h(+), m(+), s(+) ); 2. ...

  10. netty2---服务端和客户端

    客户端: package com.client; import java.net.InetSocketAddress; import java.util.Scanner; import java.ut ...