当前位置:网站首页>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;
}
}

边栏推荐
- 刷题-洛谷-P1059 明明的随机数
- GCD details
- ES6 array de duplication new set()
- 电脑里一辈子都不想删的神仙软件
- 备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
- Uncaught SyntaxError: Octal literals are not allowed in strict mode.
- The interviewer asked me: how much do you know about MySQL's storage engine?
- Concurrent programming - memory model JMM
- IM系统-消息流化一些常见问题
猜你喜欢

Friends let me see this code

adb通过Wi-Fi连接小米手机

Numpy简介和特点(一)

刷题-洛谷-P1146 硬币翻转

Redis visualizer RDM installation package sharing

6.27 uniapp项目历程

Programmer growth chapter 27: how to evaluate requirements priorities?

window unbutu20 LTS apt,wget 安装时 DNS 解析错误

Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board

Uncaught SyntaxError: Octal literals are not allowed in strict mode.
随机推荐
说说对hashcode和equals方法的理解?
百度搜索打击盗版网文站点,SEOer应该关注哪些问题?
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
刷题-洛谷-P1075 质因数分解
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
Azure Devops (XIV) use azure's private nuget warehouse
Canvas judgment content is empty
Numpy简介和特点(一)
stable_baselines快速入门
Hcip day 9 notes
IM系统-消息流化一些常见问题
vim基础操作汇总
mujoco_py中文文档
Leetcode 113. path sum II
int数组获取重复数据
Framework package merge script
Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
hcip第八天笔记
刷题-洛谷-P1146 硬币翻转