当前位置:网站首页>202. Happy number
202. Happy number
2022-07-26 17:54:00 【Crazy boy】
subject : Write an algorithm to judge a number n Is it a happy number .
「 Happy number 」 Defined as :
For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions .
Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1.
If this process The result is 1, So this number is the happy number .
If n yes Happy number Just go back to true ; No , Then return to false .
analysis :
The most important thing in this problem is to find the condition to end the cycle .
1)sum==1, End of cycle ,return true;
2)sum When repeated , End of cycle ,return false;
Code (java):
class Solution {
public boolean isHappy(int n) {
int sum =-1;
Set<Integer> set = new HashSet<>();
while(sum!=1){
if(sum != -1)n=sum;
sum =0;
while(n>0){
sum += (n%10)*(n%10);
n = n/10;
}
if(set.contains(sum))return false;
set.add(sum);
}
return true;
}
}边栏推荐
- Sequential storage structure of linear table -- sequential table
- 线性表的顺序存储结构——顺序表
- 3、 Topic communication: create your own information format
- How to set IP for layer 2 management switches
- 我们被一个 kong 的性能 bug 折腾了一个通宵
- 树形dp问题
- On the growth of data technicians
- [training Day1] Dwaves line up
- We were tossed all night by a Kong performance bug
- Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market
猜你喜欢

Spark unified memory partition

Just this time! Talk about the technical solutions of distributed system in detail

就这一次!详细聊聊分布式系统的那些技术方案

解决哈希冲突的几种方式

【集训Day2】cinema ticket
![[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension](/img/60/17cb0295f81dc580cc3ff8543ec253.png)
[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension

Spark统一内存划分

COSCon'22城市/学校/机构出品人征集令

图的遍历的定义以及深度优先搜索和广度优先搜索(一)

Tianyi cloud web application firewall (edge cloud version) supports the detection and interception of Apache spark shell command injection vulnerabilities
随机推荐
[300 opencv routines] 240. Shi Tomas corner detection in opencv
[training Day2] torchbearer
Diagram of seven connection modes of MySQL
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
1、 Header file, output format,::, namespace
就这一次!详细聊聊分布式系统的那些技术方案
AI zhetianchuan ml unsupervised learning
How to assemble a registry?
中国聚异丁烯市场研究与投资价值报告(2022版)
国际象棋机器人夹断7岁男孩手指,原因是「棋手违反安全规则」?
Analysis of interface testing
重磅!《2022中国开源发展蓝皮书》正式发布
[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security
【欧米读书会】谈谈元宇宙中的创作者经济:无限次元
GAN (Generative Adversarial Network,GAN)生成式对抗网络
2.1.2 synchronization always fails
[day3] reconstruction of roads
Week 16 OJ practice 1 calculates the day of the year
Redisdesktopmanager removes the upgrade prompt
Is it safe for Changzheng securities to open an account?