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 ...
随机推荐
- 使用zabbix-proxy
事情背景: vt上两个vps,只提供ipv6.(因为便宜嘛).而我的zabbix服务器在腾讯云.它丫的没有ipv6. 那么我没法监控它们了呀... 这咋个行呢? 想办法... 我还有另外的vps 可以 ...
- web软件测试基础系统测试简化理论
系统测试点主要如下 1.系统测试基础-2.测试对象与测试级别-3.系统测试类型-4.系统测试方法-5.系统测试之软件测试质量. 1.系统测试:是尽可能彻底地检查出程序中的错误,提高软件系统的可靠性. ...
- P2119 魔法阵
原题链接 https://www.luogu.org/problemnew/show/P2119 YY同学今天上午给我们讲了这个题目,我觉得她的思路很好,特此写这篇博客整理一下. 50分:暴力枚举 ...
- Python3读写JSON文件
JSON简介 JSON(JavaScript Object Notation)即JavaScript对象表示法,一种轻量级,通用的文本数据格式. JSON语法支持对象(Object),数组(Array ...
- pandas入门之DataFrame
创建DataFrame - DataFrame是一个[表格型]的数据结构.DataFrame由按一定顺序排列的多列数据组成.设计初衷是将Series的使用场景从一维拓展到多维.DataFrame既有行 ...
- 常用exporter下载
1.node_exporter https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter- ...
- OpenResty之ngx_lua模块的加密接口
原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...
- Qt打开文件QFileDialog
//打开Pts文件按钮点击事件void AnalysisPtsDataTool201905::OnOpenFileButtonClick(){ qDebug()<<"open f ...
- Qt编写自定义控件20-自定义饼图
前言 上次在写可视化数据大屏电子看板项目的时候,为了逐步移除对QChart的依赖(主要是因为QChart真的太垃圾了,是所有Qt的模块中源码最烂的一个,看过源码的人没有一个不吐槽,不仅不支持10W级别 ...
- Crontab的格式说明
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...