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. 学C日志

    学C历程 这里记录的都是笔记 忘记了好回头看看~ 提示 :如果在双击自己编译的程序运行时一下就消失不见了 可以在程序代码末尾加上 system("pause"); 程序就会等待到输 ...

  2. mvc路由,mvc区域

    1.路由在进行匹配时,会默认匹配第一个路由信息 2.路由规则的{control}和{action}时不能改变的 3.路由规则可以中间字符可以随便定义,但是{control}和{action}必须使用一 ...

  3. photoshop,黑白转彩色单色

    方法一: 打开 色相/饱和度 面板,勾选上 着色, 然后调节 明度 滑块. 方法二: 前面方法的缺点是会将修改直接塌陷到图层,如果想不塌陷,可以使用色相蒙板: 同样勾选 着色

  4. [原]php远程odbc连接sqlsvr数据库,自定义端口,命名实例的连接方式

    远程odbc连接sqlsvr数据库,自定义端口,命名实例的连接方式,默认如果不修改的话sqlsvr的端口号是1433,默认实例名就是机器名,,如果既用了命名实例,又改了默认端口,改怎么连接数据库呢? ...

  5. 为了方便可灌入自定义方法AppendLog 比如File

    说起到qt的编译,真是领人痛心啊,不仅编译选项繁多,而且编译时间比较久,总是能使想编译qt源码的人望而却步,呵呵...我就是其中一个,不知道从什么时候开始就想着把qt的源码编译一下,也尝试过几次,但都 ...

  6. IOS-细节错误

    当页面显示时一直奔溃,错误提示-[UICachedDeviceWhiteColor pointSize]: unrecognized selector sent to instance 原因是设置导航 ...

  7. GDI+ 操作TIFF ccitt t.6 压缩

    Bitmap Bi=new Bitmap("C:\img.tif"); SaveFileDialog sfdlg = new SaveFileDialog(); sfdlg.Fil ...

  8. Python多线程、进程入门1

    进程是资源的一个集合, 1.一个应用程序,可以有多进程和多线程 2.默认一个程序是单进程单线程 IO操作使用多线程提高并发 计算操作使用多进程提高并发 进程与线程区别 1.线程共享内存空间,进程的内存 ...

  9. Python入门2

    字符串操作 字符串是语言中使用最多的,下面我们来看看python为字符串提供哪些方法: 1.upper().lower().title() 这3个方法都是返回一个新的字符串.重要性:** name = ...

  10. GPU硬件加速相关

    从android3.0开始,2D渲染开始支持硬件加速,即在view的Canvas上的绘图操作可以用GPU来加速. 硬件加速会使app消耗更多的内存. 如果配置文件中,Target API level  ...