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 ...
随机推荐
- java集合类-Set接口
Set集合 Set集合中的对象不按特定的方式排序,只是简单的把对象放入集合中,但是不能包含重复对象. Set集合由Set接口和Set接口的实现类组成,Set接口继承与于Collection接口 Set ...
- ImportError: No module named pytz
xxx@hostname:/opt/xx/cc$ python manage.py runserver Traceback (most recent call last): File , in < ...
- Python2.7编码问题:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position...解决方法
解决方法: 在Python程序最前面加上以下语句: import sys reload(sys) sys.setdefaultencoding('utf-8')
- vue-cli脚手架构建项目
vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. 一.安装node.js 二.安装vue-cli npm install -g vue-cl ...
- Python学习日记(六)——内置函数和文件操作(lambda)
lambda表达式 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'prime' else: name = 'c ...
- 深入分析JAVA IO(BIO、NIO、AIO)
IO的基本常识 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否完成 2.异步 用户触发IO操作以后,可以干别的事,IO操作完成以后再通知当前线程继续处理 3.阻塞 当一个线程调用 r ...
- fiddler 基本知识(一)
工作原理: fiddler,原理是在本机开启了一个http代理服务器,默认监听127.0.0.1:8888. 打开fiddler之后,会帮我们设置ie代理,虽然只设置了ie,但整个计算机都通过整个代理 ...
- IDEA 好用的插件
IDEA 好用的插件 非自带的一些自用插件. Alibaba java Coding Guidelines 阿里出的java规范,支持eclipse和Idea,支持实时扫描,规范代码,养成良好习惯.推 ...
- java工厂模式的测试
/** * 工厂模式的测试 * <p> * 工厂方法模式 * 工厂方法模式是简单工厂模式的进一步抽象化和推广,工厂方法模式里不再只由一个工厂类决定那一个产品类应当被实例化,这个决定被交给抽 ...
- pandas总结
### 一.创建对象 # 1.可以通过传递一个list对象来创建一个Series,pandas会默认创建整型索引: # s=pd.Series([1,3,5,np.nan,6,8]) # print ...