一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

(二)解题

题目大意:判断一个数是不是happy number。给定一个数,对其每一位上的数求平方和,得到的数一直重复球平方和,如果在这个过程中得到的平方和为1,则该数数happy number;反之则不是。

解题思路:每次对结果求每一位的平方和,判断与1相不相等。但是要考虑死循环的情况。求出来的平方和可能存在始终都不等于1,就陷入了死循环。

我们举个例子:2,一次算平方和得到:4,16,37,58,89,145,42,12,5,25,29,85,89,从这里开始循环了。

所以,我们保存下来每次算的平方和,如果算出来的平方和与前面某一个相等,就代表陷入死循环了。

考虑到这点,就可以写出如下代码:

class Solution {
public:
    bool isHappy(int n) {
        set<int> exist;
        int num = n;
        while(num!=1){//num不等于1
            int temp = num;
            int sum = 0;
            while(temp){//计算平方和
                sum+=(temp%10)*(temp%10);
                temp/=10;
            }
            if(sum==1) return true;//等于1表示是happy number
            else{//判断与之前的平方和是否有重复
                auto iter = exist.find(sum);
                if(iter!=exist.end()) break;//有重复就跳出循环
                else exist.insert(sum);//没有就加入到exist集合中
            }
            num = sum;//继续计算平方和
        }
        return num==1;//判断与1想不想等,此处排除小于等于0的数
    }
};

【一天一道LeetCode】#202. Happy Number的更多相关文章

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

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

  2. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  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. Java for LeetCode 202 Happy Number

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

  7. (easy)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. 40. leetcode 202. Happy Number

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

  10. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. 在右键中添加以管理员运行CMD命令提示符 (进化版)

    直接代码,转过来的 20180316更新添加快捷键A,点右键按A即可: Windows Registry Editor Version 5.00 ; Created by: Shawn Brink ; ...

  2. JSP 基本语法

    1 JSP 的由来 servlet产生后,存在很大的问题,为了表现页面的效果,需要输出大量的HTML 语句,表现为一个个字符串,不仅利于开发,也不利于后期的维护,由此产生了JSP.主要用于将Servl ...

  3. eclipse中安装freemarker插件及ftl使用freemarker编辑器

    http://www.07net01.com/2015/08/895212.html eclipse中安装freemarker插件及ftl使用freemarker编辑器 在线安装的方法是:Help – ...

  4. Tomcat,eclipse热部署的三种方式

    热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB服务器前提下能让修改生效.但是对配置文件的修改除外! 怎么说呢?热部署其实用的算少了,热部署怎么说都是个人部署的,大点的公 ...

  5. SQL执行SQL语句提示 "内存不足"(insufficient memory....)的解决方法

    由于本地执行的sql script的文件太大但是本地sql的运行内存有限,当我在MSSql的工具上运行这份178M左右的脚本的时候 它会提示 如下错误(Insufficient memory to c ...

  6. vs2017 +CUDA 9.0配置

    环境: 1.Win7 64位 旗舰版 2.VS2017 3.CUDA 9.0 安装过程比较简单,直接运行在官网下载的CUDA安装包就可以了. 建议先安装VS,再安装CUDA.这样安装完之后会在VS里直 ...

  7. Bootstrap3 栅格系统-媒体查询

    在栅格系统中,我们在 Less 文件中使用以下媒体查询(media query)来创建关键的分界点阈值. /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 B ...

  8. [BBS]搭建开源论坛之Jforum搭配开源CKEDITOR

    本文作者:sushengmiyan 本文地址:http://blog.csdn.net/sushengmiyan/article/details/47946065 使用默认的编辑器的时候,格式都无法保 ...

  9. proc文件系统探索 之 根目录下的文件[二]

    包括对proc根目录下stat,uptime,swaps三个文件的解析. /proc/stat 文件包含了系统启动后的一些系统统计信息. Cat /proc/stat: cpu 77781 1077 ...

  10. Latex 文本编辑技巧

    临时取消首行缩进 \noindent 生成随机文本 \usepackage{lipsum} \begin{document} \lipsum \end{document} 多栏模式 \usepacka ...