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

边栏推荐
- Error of Tencent cloud [100007] this env is not enable anonymous login
- 互斥锁、自旋锁、读写锁……理清它们的区别和应用
- Jupyter Notebook介绍
- mujoco_py中文文档
- 【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
- HTTP cache tongtianpian, there may be something you want
- Numpy简介和特点(一)
- The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
- What is your revenue rank among global developers in 2022?
- @wrap 装饰器
猜你喜欢

6.27 uniapp project history

G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image

Applet H5 get mobile number scheme

Programmer growth chapter 27: how to evaluate requirements priorities?

vim基础操作汇总

Friends let me see this code

刷题-洛谷-P1075 质因数分解

0716RHCSA

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

运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
随机推荐
2022年下半年软考初级程序员备考
Design and principle of thread pool
NoSQL,关系型数据库,行列数据库对比、类比
Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework
adb通过Wi-Fi连接小米手机
Friends let me see this code
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
hcip第九天笔记
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
Applet H5 get mobile number scheme
Hcip seventh day notes
0716RHCSA
MXNet对DenseNet(稠密连接网络)的实现
嵌入式代码如何进行重构?
并发编程之AQS
刷题-洛谷-P1151 子数整数
Concurrent programming - memory model JMM
Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
VIM basic operation summary
Hcip day 10 notes