202. Happy Number

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返回true,参数出现过则返回false.

代码如下:

 class Solution {
public:
int sum;
vector<int> re;
bool isHappy(int n) {
if(n == )
{
re.clear();
return true;
}
else
{
int count = ;
for(int i = ; i < re.size(); i++)
{
if(n == re[i])
{
count++;
}
if(count > )
{
return false;
}
}
}
sum = ;
while(n)
{
sum += (n%) * (n%);
n /= ;
}
re.push_back(sum);
return isHappy(sum);
}
};

leetcode 202的更多相关文章

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

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

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

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

  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. Java实现 LeetCode 202 快乐数

    202. 快乐数 编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过 ...

  6. LeetCode 202 Happy Number

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

  7. Java for LeetCode 202 Happy Number

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

  8. (easy)LeetCode 202.Happy Number

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

  9. Java [Leetcode 202]Happy Number

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

随机推荐

  1. jquery checkbox的相关操作——全选、反选、获得所有选中的checkbox

    1.全选 $("#btn1").click(function(){ $("input[name='checkbox']").attr("checked ...

  2. (转) Deep Learning Resources

    转自:http://www.jeremydjacksonphd.com/category/deep-learning/ Deep Learning Resources Posted on May 13 ...

  3. 如何使用vmware-vdiskmanager增加磁盘空间

    VMware Virtual Disk Manager Usage: vmware-vdiskmanager.exe OPTIONS <disk-name> | <mount-poi ...

  4. 模拟discuz发帖的类实现

    一直想弄个discuz的数据采集程序,这2天研究了下discuz发帖涉及的几个数据库表,这里分享一下自己的处理方法. discuz发表主题设计的几个表:(这里列出了主要的几个相关的表) 1.主题表 p ...

  5. 自己动手制作CSharp编译器

    在你喜欢的位置(如F盘根目录)新建一个文件夹,并命名为“CSharp开发环境”.找到或下载C#编译器组件(csc.exe和cscui.exe),并放在先前建立的文件夹中.该组件的一般位置在C盘的.NE ...

  6. Head First设计模式之装饰者模式(Decorator Pattern)

    前言: 本节将深度讨论继承滥用问题,将会学到使用对象组合的方式,在运行时装饰类,在不修改任何底层代码的情况下,给对象赋予新的职责. 1.    基本需求:咖啡连锁店业务扩张需要重新设计订单系统 背景: ...

  7. 在 Web 项目中应用 Apache Shiro

    Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web ...

  8. maven的阿里镜像

    偶然发现maven有了阿里镜像 vim ~/.m2/setting.xml <mirrors> <mirror> <id>alimaven</id> & ...

  9. USVN

    我们最近将快盘上的东西迁移到了svn上,因为快盘总是不会不小心删掉或者修改了某些文件.为了能保留历史记录我们统一迁移到svn上.为了方便权限管理,我对比了几个svn的权限管理工具,最后觉得还是usvn ...

  10. Linux环境下SVN服务器端的安装与配置

    最近尝试了下在Linux(CentOS6.5)环境下安装与配置SVN服务器端,安装过程中碰到了一些问题,参看了网友们分享的一些心得,并通过自己实际的操作,最终安装与配置成功!总的来说网上的说法芸芸,大 ...