当前位置:网站首页>leetcode202---快乐数
leetcode202---快乐数
2022-07-25 13:38:00 【Peihj2021】
leetcode 202-- 快乐数
链接: 快乐数
题目描述


思路分析
1、首先我们需要理解题目的意思,就是给的数字按照其个十百可以分别进行进行平方。
2、如果平方后的数字最终结果为1,那么这个数是快乐数。
3、如果最后结果中出现了重复的数字,那么这个数字就不是快乐数,如何判断数字是否重复,那必然得通过hashset。
代码演示
class Solution {
public boolean isHappy(int n) {
HashSet<Integer> hashSet = new HashSet<>();
while (n != 1 && !hashSet.contains(n)){
hashSet.add(n);
n = getNextnumber(n);
}
return n == 1;
}
public int getNextnumber(int n){
int res = 0;
while(n > 0){
int tem = n % 10;
res += tem*tem;
n = n / 10;
}
return res;
}
}

边栏推荐
- 刷题-洛谷-P1089 津津的储蓄计划
- mujoco_ Py Chinese document
- pytest.mark.parametrize及mock使用
- 0716RHCSA
- Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
- QGIS loading online map: Gaode, Tiandi map, etc
- Numpy quick start
- The migration of arm architecture to alsa lib and alsa utils is smooth
- Hcip day 6 notes
- Error: cannot find or load main class XXXX
猜你喜欢

MXNet对DenseNet(稠密连接网络)的实现

Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)

Excel添加按键运行宏

pytest.mark.parametrize及mock使用

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

Audio and video technology development weekly | 255

Jupyter Notebook介绍

vim基础操作汇总

刷题-洛谷-P1085 不高兴的津津

刷题-洛谷-P1161 开灯
随机推荐
互斥锁、自旋锁、读写锁……理清它们的区别和应用
In order to improve efficiency, there are various problems when using parallelstream
Numpy快速入门
How to realize the configuration method of user password free login?
为提高效率使用ParallelStream竟出现各种问题
全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
uniapp处理后台传输图片
What is your revenue rank among global developers in 2022?
备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
window unbutu20 LTS apt,wget 安装时 DNS 解析错误
GCD details
Jupyter Notebook介绍
刷题-洛谷-P1046 陶陶摘苹果
The interviewer asked me: how much do you know about MySQL's storage engine?
hcip第八天实验
IM系统-消息流化一些常见问题
0710RHCSA
Framework package merge script
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
Pycharm cannot input Chinese solution