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 ...
随机推荐
- centos7下glances系统监控的安装
yum install epel* -y yum install python-pip python-devel -y yum install glances -y 启动>>glances
- Java分布式互联网架构/微服务/高性能/springboot/springcloud2018年10月16日直播内容
2018年10月16日直播内容 架构师揭秘springboot对springmvc的自动配置原理 直播地址:https://ke.qq.com/course/179440?tuin=9b386640 ...
- Ubuntu18.04 桌面系统的个人吐槽(主要是终端)
装了Ubuntu18.04,桌面换风格了,使用中最大的感觉是终端切换非常反人类,可能是我还没有摸清门路.原先习惯用Alt+Tab快捷键切不同终端以及不同窗口的,现在Alt+Tab时多个终端会归成一个图 ...
- Python 自学笔记(一)
1.打印函数 1-1.print()函数 1-1-1.引号的用法 一.单引号与双引号:直接输出 二.三引号:保留原来的格式 1-2.转义字符 转义字符是一种特殊的字符常量,在编程语言中,我们用转义字符 ...
- oracle OPEN FOR [USING] 语句
目的: 和ref cursor配合使用, 可以将游标变量分配给不同的SQL (而不是在declare中把游标给定死), 增加处理游标的灵活性语法: declare type type_c ...
- vim 快捷键 清空文件所有内容
vim清空文件所有内容 在使用vim编辑器的时候,有时候编辑一个文件,而文件内容比较多,如果需要快速清空整个文件,可以使用一下命令: 在命令模式下,首先执行 gg 这里是跳至文件首行 再执行: dG ...
- start开启服务的生命周期
* 完整生命周期:onCreate()-->onStartCommand()-->onDestroy() * 开启服务:onCreate()-->onStartCommand() * ...
- CentOS下yum安装mysql
其实跟windows下安装过程差别不大,就是下载为了方便使用了yum的方式.安装前先确认下系统是否还安装mysql,卸载是否不完全,再去官网(http://dev.mysql.com/download ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_14.RabbitMQ研究-与springboot整合-搭建环境
我们选择基于Spring-Rabbit去操作RabbitMQ https://github.com/spring-projects/spring-amqp 使用spring-boot-starter- ...
- python3 super().__init__() 和 __init__() 的区别
1.单继承 super().__int__()和 Base.__init__(self)是一样的, super()避免了基类的显式调用. class Base(object): def __init_ ...