问题描述

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. vue中父组件如何监听子组件值的变化

    vue中我们会遇到很多父子组件通信的需求, 下面简单列一下,父子组件通信的几种情况 1:父组件向子组件传值:使用prop向子组件传值: 2:子组件实时监听父组件传来的值的变化:使用watch去监听父组 ...

  2. C语言学习笔记5-程序结构

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/50752148 作者:jadeshu   邮箱: jades ...

  3. Java并发指南8:AQS中的公平锁与非公平锁,Condtion

    一行一行源码分析清楚 AbstractQueuedSynchronizer (二) 转自https://www.javadoop.com/post/AbstractQueuedSynchronizer ...

  4. 通过xshell在本地win主机和远程linux主机传输文件

    1.下载和安装xshell此处不再介绍 2.安装lrzsz的软件 yum install lrzsz 3.通过xshell上传文件 只需要在XShell的菜单中点击File – Transfer – ...

  5. 设置Win10默认启动的Linux子系统版本,启动指定Linux发行版

    设置Win10默认启动的Linux子系统版本,启动指定Linux发行版   MS酋长一年前已经与大家分享了启用“适用于Linux的Windows子系统(WSL)”的方法,但当时所能安装的只有由Cano ...

  6. CentOS下yum安装jdk

    jdk安装极其简单,因为java应用太广泛.先看下系统中是否已安装,已安装了先卸载,没安装则直接通过yum一步到位安装即可. 1.看本机是否已有jdk # java -version -bash: j ...

  7. django安装xadmin中出现的报错汇总

    报错一:ModuleNotFoundError: No module named 'django.core.urlresolvers' ModuleNotFoundError: No module n ...

  8. Qt编写自定义控件24-图片轮播控件

    一.前言 上一篇文章写的广告轮播控件,采用的传统widget堆积设置样式表做的,这次必须要用到更高级的QPainter来绘制了,这个才是最高效的办法,本控件参考雨田哥的轮播控件,经过大规模的改造而成, ...

  9. NuGet修改packages目录/迁移缓存文件夹

    如图,以下是NuGet默认配置 打开C:\Program Files (x86)\NuGet\Config目录的Microsoft.VisualStudio.Offline.config可以看见如下配 ...

  10. Nginx+Keepalived双主架构实现

    Keepalived+Nginx实现高可用Web负载均衡 Master 192.168.0.69 nginx.keepalived Centos7.4backup 192.168.0.70 nginx ...