leetcode 202
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的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java实现 LeetCode 202 快乐数
202. 快乐数 编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过 ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
随机推荐
- MongoDB学习笔记九:分片
分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程.有事也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机既可以存储更多的数据 ...
- MaskEdit 使用方法
它有一个 MaskEdit 属性(注意,属性) 打开后有一个 Input Mask 编辑框 格式符意义 L 允许输入英文字母,且一定要输入 l 允许输入 ...
- Asp.net中文件的压缩与解压
这里笔者为大家介绍在asp.net中使用文件的压缩与解压.在asp.net中使用压缩给大家带来的好处是显而易见的,首先是减小了服务器端文件存储的空间,其次下载时候下载的是压缩文件想必也会有效果吧,特别 ...
- link与import的区别
- (C++) LNK2019: unresolved external symbol.
Error 33 error LNK2019: unresolved external symbol "\xxx.obj yyy.Native 仔细看看错误信息,后来发现尽然是构造函数的一个 ...
- Eclipse调试时附加匹配版本的JAR包源码:Edit Source Loopup
- [Linux] IP绑定解释 BindIp
一.缘由: 今天安装Mongodb,本来想限制只能内网或者某几台机器可以访问,看到配置文件有个net.bindIp选项, 就自以为是的认为,他可以像nginx那样限制访问来源IP,其实大错特错.这里配 ...
- 使用compass编译sass
1.初始化项目 compass create test(项目名称),会在当前目录下创建test子目录,test的子目录下有config.gb文件,sass和stylesheets文件夹. 2.编写sa ...
- step by step install Caffe
Caffe + Ubuntu 14.04 + CUDA 8 + cudnn 8.0+Ananconda3+opencv3.0.0 //由于本人使用的是英文版ubuntu,没装汉语输入法,暂时就用蹩脚的 ...
- 关于CSS学习的第一章
1.CSS三种书写的方式:嵌入式.外链式.行内式 嵌入式就是将CSS写入在<style></style> 外链式将外面的CSS文件通过HTML中的标记链接过来:<link ...