LC 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:
Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
参考答案
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> temp;
while(n != ){
if(temp.find(n) == temp.end())
temp.insert(n);
else
return false;
int sum = ;
while(n!=){
sum += pow(n%,);
n = n/;
}
n = sum;
}
return true;
}
};
答案分析
答案分成两部分:扔进hashset里,计算结果。
第一部分。把每次的n都加入hashset中,使用 hashset.end() 判断是否为空。如果重复了,那么说明算了一圈算回来了,不是 happy number。
第二部分。和例子一样,每次sum=0,然后 update the value of n.
最后 n 成为了 1, 说明是 happy number。
LC 202. Happy Number的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- 202. Happy Number 平方循环数
[抄题]: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- 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 ...
- 【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 ...
- 202. Happy Number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...
随机推荐
- WebGL的shader
WebGL的shader(着色器)有2种:vertexShader(定点着色器)和 fragmentShader(片段着色器) 顶点着色器:定义点的位置.大小 片元着色器:定义画出来的物体的材质(颜色 ...
- html2canvas原理
html2canvas有2种模式,一种是利用foreignObject,一种是纯canvas绘制 1.foreignObject到canvas 步骤: 1.把要截图的dom克隆一份,过程中把getCo ...
- 1-7HSB色彩模式
http://www.missyuan.com/thread-350721-1-1.html HSB色彩模式色相hue.饱和度saturation.明度brightness 在HSB模式中,S和B的取 ...
- NSString的导出
字符串的导出,写到某个文件中去 void stringExport(){ NSString *str=@"123456"; //if file not exist will not ...
- Zhejiang Provincial Collegiate Programming Contest + ZOJ Monthly
题目链接:https://vjudge.net/contest/152802#overview. 前五题以前做过了.不过还是没能全A= =. 前三题水题,略过. 第四题是找规律,暴力打表找一下循环节即 ...
- Redis字符串(String)
1.set SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值 value 关联到 key 可选参数: EX second :设置键的 ...
- spring cloud学习
https://gitee.com/ylimhhmily/SpringCloudTutorial/tree/master hystrix配置不生效问题https://blog.csdn.net/lvy ...
- [Phoenix] Mix 命令
mix phx.gen.html 命令生成模板: # 其中 name 和 age 是 schema 字段名称,后面跟的是类型 # 下面这样的写法,会生成 controller 和 service 层的 ...
- Event---事件详解
1.焦点事件 焦点:使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 可以通过以下方式给元素设置焦点: 点击.tab.js 不是所有元素都能够接收焦点的,能够响应用 ...
- python3 django项目从项目中导出依赖包
1. 在项目的根目录中使用mac终端执行命令, pip3 freeze > requirements.txt #requirements.txt只是个名字可以随便起,一般默认为requireme ...