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

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

思路:把一个数每一位平方后加和得到一个新的数,然后继续对这个新的数做同样的操作。如果最后能得到一个1,则最原始的数就是一个”Happy Number”。如果在还没达到1的过程中出现循环,则这个数肯定不是。用到C++ 的set。set.count()判断一个数是否在set中,set.insert(),向set 中插入元素。INT_MAX,表示最大整数。

class Solution {
public:
bool isHappy(int n) {
if(n==)
return false;
set<long> s;
while(n<=INT_MAX)
{
if(n==)
return true;
if(s.count(n)>)
return false;
s.insert(n);
n=digitSquare(n);
}
return false; }
long digitSquare(long num)
{
long r=;
int t=;
while(num)
{
t=num%;
r+=t*t;
num/=;
}
return r;
}
};

40. leetcode 202. Happy Number的更多相关文章

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

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

  2. LeetCode 202. Happy Number (快乐数字)

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

  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

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

  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. Java [Leetcode 202]Happy Number

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

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

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

  9. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

随机推荐

  1. codeM美团编程大赛初赛B轮D题

    [编程题] 模 时间限制:1秒空间限制:32768K 给定四个正整数a,b,c,k,回答是否存在一个正整数n,使得a*n在k进制表示下的各位的数值之和模b为c.输入描述:第一行一个整数T(T < ...

  2. nvarchar 和varchar区别

    有时候设计字段的时候,碰到nvarchar和varchar时候,是有点犹豫.所以今天就来探个究竟把. (一)  varchar是非Unicode可变长度类型,nvarchar是Unicode编码可变长 ...

  3. [leetcode-557-Reverse Words in a String III]

    Given a string, you need to reverse the order of characters in each word within a sentence whilestil ...

  4. 1.免费安装myeclipse 10以及破解

    1.材料准备 jdk1.8.0_101 网盘地址链接: http://pan.baidu.com/s/1ge8Jaz5 密码: qb6v myeclipse-10.6-offline-installe ...

  5. meta小结

    mate 标签定义及使用说明 元数据(Metadata)是数据的数据信息. 标签提供了 HTML 文档的元数据.元数据不会显示在客户端,当时会被浏览器解析. META元素通常用于指定网页的描述,关键词 ...

  6. Ext & Java 上存图片 Demo

    Ext & Java 上存图片 Demo Ext <html> <head> <script id="microloader" type=&q ...

  7. 我的学习之路_第二十七章_jQuery

    jQueryjs类库 把常用对象或者方法封装起来,让我们写代码效率更高 1.jQuery 2.extjs [jQuery入门] jQuery的引入: 通过script标签的src属性引入 入门: 获取 ...

  8. 第二章:1.0 Django 入门和开发环境

    1. 选择 Django Web框架来做Web接口开发,主要原因是由于学习资料丰富,便于学习. 2. Django 对 python 版本的支持情况. Django 的版本在 1.8 ,1.9 , 1 ...

  9. 通用JSONHelp 的通用的封装

    1. 最近项目已经上线了 ,闲暇了几天 想将JSON  的序列化 以及反序列化进行重新的封装一下本人定义为JSONHelp,虽然Microsoft 已经做的很好了.但是我想封装一套为自己开发的项目使用 ...

  10. CSS3-loading动画(二)

    上次分享了四个CSS3的加载动画,今天继续(标题接上一次). 在线demo:http://liyunpei.xyz/loading.html   (持续更新) 请注意:代码中的关键帧动画有的用的lin ...