问题描述

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: 

Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

参考答案

class Solution {
public:
bool isHappy(int n) {
unordered_set<int> temp;
while(n != ){ if(temp.find(n) == temp.end())
temp.insert(n);
else
return false; int sum = ;
while(n!=){
sum += pow(n%,);
n = n/;
} n = sum;
}
return true; }
};

答案分析

答案分成两部分:扔进hashset里,计算结果。

第一部分。把每次的n都加入hashset中,使用 hashset.end() 判断是否为空。如果重复了,那么说明算了一圈算回来了,不是 happy number。

第二部分。和例子一样,每次sum=0,然后 update the value of n.

最后 n 成为了 1, 说明是 happy number。

LC 202. Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. 202. Happy Number 平方循环数

    [抄题]: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  3. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  4. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  5. Java for LeetCode 202 Happy Number

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

  6. (easy)LeetCode 202.Happy Number

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

  7. 【LeetCode】202 - Happy Number

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

  8. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  9. 202. Happy Number

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

随机推荐

  1. Simple Problem with Integers(POJ 3486)

                                                                  A Simple Problem with Integers Time Li ...

  2. Ubuntu14.04 dd命令克隆系统镜像安装到另一台机器上

    linux系统如果想做备份还原,使用ghost的时候经常出现问题,后来发现可以直接使用dd命令完成硬盘的克隆和还原.当拷贝完硬盘后,就可以拿这个硬盘放到其它设备上跑了.也就是完成了“烧写”了. 用U盘 ...

  3. 记录下我用Jenkins打包碰到的坑

    使用Andorid Studio 打包都是正常的,但是使用Jenkins自动打包一直报错,尝试过网上的各种方案,依然都不行,报错如下. FAILURE: Build failed with an ex ...

  4. white-space 标签 使用

    white-space MSD定义为: 是用来设置如何处理元素中的空白 其使用场景有很多,比如:横向滑动,超出显示省略号,输出空格显示空格等 1.横向滑动 在写手机页面的过程中,我想大部分人都遇到横向 ...

  5. 重读APUE(5)-文件权限

    文件,目录,权限 1. 用名称打开任一个类型的文件时,对该名字中包含的每一个目录,包括它可能隐含的当前工作目录都应该具有执行权限:这就是目录执行权限通常被称为搜索位的原因: 例如:为了打开文件/usr ...

  6. Mybatis按照SQL查询字段的顺序返回查询结果,使用resultType="java.util.LinkedHashMap"

    在使用Mybatis开发时,Mybatis返回的结果集就是个map,当返回map时只需要做好SQL映射就好了,减少了代码量,简单便捷,缺点是不太方便维护,但是写大量的vo类去返回也挺累的,这个看你个人 ...

  7. Bootstrap form-group and form-control

    https://github.com/twbs/bootstrap/blob/21f3375f21e9a7a5155d0cd783fd2bc7aeee8485/scss/_forms.scss htt ...

  8. 图像模糊C均值聚类分割代码

    转自:直觉模糊C均值聚类与图像阈值分割 - liyuefeilong的专栏 - CSDN博客 https://blog.csdn.net/liyuefeilong/article/details/43 ...

  9. centos7.4出现yum command not found

    购买的云服务器运行yum命令出现yum command not found. 通过将云主机自带的yum和python卸载掉,并且同时需要关注/usr/bin/yum文件的首行解释.我定义其为" ...

  10. vue的路由认识

    this.$router.options.routes //获得整个路由路径对象 this.$route.matched //获得当前路由的路径对象