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

边栏推荐
- 包管理 apt,dpkg
- ES6 array de duplication new set()
- mysql 01: source命令
- 0715RHCSA
- Hcip day 10 notes
- Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
- Explain the precision of floating point numbers in detail
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
- 并发编程之AQS
- 0716RHCSA
猜你喜欢
随机推荐
0715RHCSA
Blocking queue for concurrent programming
@Wrap decorator
Immortal software in the computer that I don't want to delete all my life
Redis visualizer RDM installation package sharing
My creation anniversary
Canvas judgment content is empty
Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?
Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
Canal realizes MySQL data synchronization
Install oh my Zsh
刷题-洛谷-P1089 津津的储蓄计划
Hcip eighth day experiment
Applet enterprise red envelope function
Excel record macro
「数字安全」警惕 NFT的七大骗局
Applet H5 get mobile number scheme
刷题-洛谷-P1161 开灯
Turn off automatic update when brew executes commands








