一天一道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. SpringCloud学习之Zuul统一异常处理及回退

    一.Filter中统一异常处理 其实在SpringCloud的Edgware SR2版本中对于ZuulFilter中的错误有统一的处理,但是在实际开发当中对于错误的响应方式,我想每个团队都有自己的处理 ...

  2. python MySQLdb pymsql

    参考文档 https://www.python.org/dev/peps/pep-0249/#nextset 本节内容 MySQLdb  pymysql MySQLdb和pymysql分别为Pytho ...

  3. RabbitMQ-Spring AMQP

    上篇文章RabbitMQ基础入门学习了rabbitMQ一些基础的api,当然spring也在原生代码的基础上做了更多的封装,这篇文章就基于spring-rabbit,学习一下spring的实现. p. ...

  4. TensorFlow + Keras 实战 YOLO v3 目标检测图文并茂教程

    运行步骤 1.从 YOLO 官网下载 YOLOv3 权重 wget https://pjreddie.com/media/files/yolov3.weights 下载过程如图: 2.转换 Darkn ...

  5. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  6. CNN中的经典结构之AlexNet

    AlexNet的基本结构 Alexnet是由5个卷积层和三个全连接层组成,一共8个权重层(池化层不是权重层因为其没有参数),其中ReLU激活函数作用在每个卷积层和全连接层上,在第一个卷积层和第二个卷积 ...

  7. CodeForces - 766B Mahmoud and a Triangle

    [题意概述] 给定一串数,从中挑三个,判断能否组成一个有正面积的三角形,如果能就输出YES,否则就输出NO [题目分析] 将 n 个数从大到小进行排列,三个三个往下去判断,只要后两个的和比第一个大的时 ...

  8. 基于Python预测股价

    ▌实现预测的Stocker工具 Stocker是一款用于探索股票情况的Python工具.一旦我们安装了所需的库(查看文档),我们可以在脚本的同一文件夹中启动一个Jupyter Notebook,并导入 ...

  9. 粗糙的es6 -> es5转换正则集

    (r'() => {}','function () {return {}}'), # (r'\{\.\.\.(.+?)\}','Object.assign({}, \\1)') , # (r'( ...

  10. Redis之(二)数据类型及存储结构

    Redis支持五中数据类型:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sortedset:有序集合). Redis定义了丰富的原语命令,可以直接与Redis ...