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. 关于javascript中闭包的理解

    闭包就是能够读取其他函数内部变量的函数. 在javascript中,只有函数内部的子函数可以读取局部变量,因此,我理解闭包就是定义在一个函数内部的函数. 例子: var f1 = function() ...

  2. 【转载】.NET模拟POST登录并保持登录状态

    好了,还是由于工作需要 要登录一个网站并且模拟点击下载某些东西 原理就是先对一个地址(地址是用户名和密码输入框所在的form的action对应的页面)进行POST提交用户名和密码(不考虑验证码,当然验 ...

  3. Servlet访问第一次500,刷新后404的解决办法

    第一次运行错误:HTTP Status 500 -  --------------------------------------------type Exception reportmessage  ...

  4. Process的Waitfor() 引起代码死锁

    Java用process调用c#的exe后,process.waitfor(). exe执行会停在某处.据说是waitfor引起的exe子线程死锁. 先存一个链接 http://yearsaaaa12 ...

  5. 如何区分Babel中的stage-0,stage-1,stage-2以及stage-3(二)

    上一篇文章我们介绍了法力无边的stage-0 和 包罗万象的stage-1, 现在我们来介绍下 stage-2 和 stage-3 深藏不露的stage-2 为什么说 stage-2深藏不露呢,因为它 ...

  6. 基于服务(Web Service)的文件管理Winform程序实现

    1.       描述 面向服务的体系结构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.接口是采用中立的方式进行定义的,它应该独立于实现 ...

  7. Visual studio 类视图和资源视图不显示的问题

    关于Visual studio 类视图和资源视图不显示的问题 解决方法: 1. 工具—选项—文本编辑器—C/C++—高级,浏览/导航下的禁用数据库选项置为False; 2. 输入命令:devenv / ...

  8. IOS自定义表格UITableViewCell

    在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了. 1.在storyboard中 给一个ViewController的tabi ...

  9. ARM指令分类及其寻址方式

    ARM指令分类及其寻址方式 一:ARM指令的分类 ARM指令集可以分为以下6类: •跳转指令: •数据处理指令: •程序状态寄存器(PSR)传输指令: •load/store指令: •协处理器指令: ...

  10. [Maven]Maven非常详细完整的教程

    请参考:http://blog.csdn.net/u012152619/article/category/6239920 第四部分已看完 另外也可参看简书:http://www.jianshu.com ...