当前位置:网站首页>Happy number of leetcode topic analysis
Happy number of leetcode topic analysis
2022-06-23 08:43:00 【ruochen】
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
- Use set Calculate the result of each calculation , If it is 1 It is Happy Number; No 1 And in set There has been , Is not returned false; Otherwise, add the result to set.
- Utilization property , Special numbers “4” At some point , There is 4 Of Number Definitely not Happy.
public boolean isHappy(int n) {
if (n < 1) {
return false;
}
if (n == 1) {
return true;
}
Set<Integer> set = new HashSet<Integer>();
set.add(n);
while (true) {
int s = 0;
while (n > 0) {
s += (n % 10) * (n % 10);
n /= 10;
}
System.out.println(s);
if (s == 1) {
return true;
} else if (set.contains(s)) {
return false;
}
set.add(s);
n = s;
}
} public boolean isHappy2(int n) {
if (n < 1) {
return false;
}
if (n == 1) {
return true;
} else if (n == 4) {
return false;
}
int s = 0;
while (n > 0) {
s += (n % 10) * (n % 10);
n /= 10;
}
return isHappy2(s);
}边栏推荐
- How to mine keywords and improve user experience before website construction?
- Use newbeecoder UI implements data paging
- How to use the template library of barcode label software
- 3-progressbar and secondary cropping
- Kernel log debugging method
- Comprehensive analysis of news capture
- What is a dedicated server line
- Deep analysis and Simulation of vector
- [QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
- 鸿蒙读取资源文件
猜你喜欢

论文阅读【Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset】

3. Caller 服务调用 - dapr

渲染效果图哪家好?2022最新实测(四)

Self organizing map neural network (SOM)

RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?

自组织映射神经网络(SOM)

Why use growth neural gas network (GNG)?

Talk about the implementation principle of @autowired
![[paper notes] catching both gray and black swans: open set supervised analog detection*](/img/52/787b25a9818cfc6a1897af81d41ab2.png)
[paper notes] catching both gray and black swans: open set supervised analog detection*

Object. Defineproperty() and data broker
随机推荐
谈谈 @Autowired 的实现原理
Hongmeng reads the resource file
XSS via host header
[QNX Hypervisor 2.2用户手册]6.1 使用QNX Hypervisor系统
typeScript的介绍与变量定义的基本类型
Spirit matrix for leetcode topic analysis
ArcLayoutView: 一个弧形布局的实现
APM performance monitoring practice of jubasha app
Chapter 1 open LDAP master-slave synchronization tower construction
4-绘制椭圆、使用定时器
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
1-渐变、阴影和文本
Leetcode topic analysis sort colors
目标检测中的多尺度特征结合方式
Analysis of JMeter pressure measurement results
Point cloud library PCL from introduction to mastery Chapter 10
986. Interval List Intersections
Basic use of check boxes and implementation of select all and invert selection functions
Azure Active Directory brute force attack
2- use line segments to form graphics and coordinate conversion