一天一道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. 环境变量方式使用 Secret - 每天5分钟玩转 Docker 容器技术(158)

    通过 Volume 使用 Secret,容器必须从文件读取数据,会稍显麻烦,Kubernetes 还支持通过环境变量使用 Secret. Pod 配置文件示例如下: 创建 Pod 并读取 Secret ...

  2. 运行C++程序是出现错误:cannot open Debug/1.exe for writing

    今天,打开VC6.0环境编了个小程序,谁知给我报了“cannot open Debug/1.exe for writing”这样一个错,然后,我就纳闷了,这是什么错丫? 想了半天,后想通,为什么会这样 ...

  3. if else与switch区别

    一.if-else 只是单纯地一个接一个比较:if...else每个条件都计算一遍: 二.switch 使用了Binary Tree算法:绝大部分情况下switch会快一点,除非是if-else的第一 ...

  4. STM32 基于定时器的PWM发生器

    脉冲宽度调制(PWM),是英文"Pulse Width Modulation" 的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术.简单一点,就 ...

  5. JSON.parse()在火狐中的BUG

      //用sessionStorage解决load页面刷新问题 { //sessionStorage.removeItem("loadInfo"); var loadInfo=de ...

  6. SVN冲突解决

    问题一.执行SVN commit时候,会生成除正常文件之外.mine..r3439 ..r3368的三个文件 .mine:是自己要提交的版本 .r3439:在别人之前提交的版本 .r3368:初始版本 ...

  7. 初识RabbitMQ系列之二:下载安装

    一:Erlang安装 因为RabbitMQ 是Erlang语言开发的,所以首先要装上Erlang的环境 1)下载Erlang    下载官网:http://www.erlang.org/downloa ...

  8. 3.3 声明[[],]的数组, push_back() 和 back() 的区别

    声明一个 [[],] 的二维数组: vector < vector<int> > res(1, vector<int>()); 或者 vector<vecto ...

  9. Oracle中SQL语句分类

    Oracle中SQL语句分类如下:1.DML语句 insert/delete/update/select/merge/explan plan/lock table2.DDL语句 create/atlt ...

  10. Microsoft SQL server2017初次安装与使用记录

    Microsoft SQL server2017初次安装与使用记录 学校数据库课程以Microsoft SQL server为例, 由于老师给的软件版本和我的window10不兼容,选择官网的最新版2 ...