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

边栏推荐
- ES6 array de duplication new set()
- 电脑里一辈子都不想删的神仙软件
- Uniapp handles background transfer pictures
- 【Platform IO编译Hifive1-revB】*** [.pio\build\hifive1-revb\src\setupGPIO.o] Error 1的解决办法
- 刷题-洛谷-P1075 质因数分解
- Applet starts wechat payment
- Talk about your understanding of hashcode and equals methods?
- 刷题-洛谷-P1151 子数整数
- mujoco_ Py Chinese document
- [server data recovery] HP EVA server storage raid information power loss data recovery
猜你喜欢

Immortal software in the computer that I don't want to delete all my life

The interviewer asked me: how much do you know about MySQL's storage engine?

Introduction to jupyter notebook

Error: cannot find or load main class XXXX

刷题-洛谷-P1089 津津的储蓄计划

2022年下半年软考信息安全工程师如何备考?

【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复

0710RHCSA

Any time, any place, super detective, seriously handle the case!

Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?
随机推荐
音视频技术开发周刊 | 255
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
Programmer growth chapter 27: how to evaluate requirements priorities?
mujoco_ Py Chinese document
领域驱动模型设计与微服务架构落地-模型设计
MXNet对DenseNet(稠密连接网络)的实现
Install oh my Zsh
剑指offer专项突击版第10天
Canvas judgment content is empty
Pycharm cannot input Chinese solution
6.27 uniapp project history
Applet sharing function
0720RHCSA
Introduction to jupyter notebook
int数组获取重复数据
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
mujoco_py中文文档
Canvas判断内容为空
Congestion control of TCP