当前位置:网站首页>Leetcode202 --- Happy number
Leetcode202 --- Happy number
2022-07-25 13:44:00 【Peihj2021】
leetcode 202-- Happy number
link : Happy number
Title Description


Thought analysis
1、 First of all, we need to understand the meaning of the topic , That is, the given number can be squared according to its ten hundred .
2、 If the number after the square turns out to be 1, Then this number is happy number .
3、 If duplicate numbers appear in the final result , Then this number is not a happy number , How to judge whether the numbers are repeated , It must pass hashset.
Code demonstration
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;
}
}

边栏推荐
猜你喜欢

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

leetcode202---快乐数

Error: cannot find or load main class XXXX

Uniapp handles background transfer pictures

刷题-洛谷-P1047 校门外的树

In order to improve efficiency, there are various problems when using parallelstream

Audio and video technology development weekly | 255

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

0710RHCSA

What is your revenue rank among global developers in 2022?
随机推荐
leetcode--四数相加II
MySQL 01: Source command
Redis visualizer RDM installation package sharing
Discussion on principle and application technology of MLIR
sieve of eratosthenes
Hcip day 9 notes
飞盘局有多快乐?2022年轻人新潮运动报告
刷题-洛谷-P1075 质因数分解
说说对hashcode和equals方法的理解?
Canal realizes MySQL data synchronization
Concurrent programming - memory model JMM
刷题-洛谷-P1035 级数求和
Hcip day 10 notes
并发编程之阻塞队列
mujoco_py中文文档
hcip第十天笔记
mujoco+spinningup进行强化学习训练快速入门
Basic knowledge of binary tree
刷题-洛谷-P1152 欢乐的跳
mysql 01: source命令