当前位置:网站首页>Happy number [fast and slow pointer of ring PROBLEMS]
Happy number [fast and slow pointer of ring PROBLEMS]
2022-07-01 22:54:00 【REN_ Linsen】
Speed pointer
Preface
Analyze the meaning of the question , There are problems related to the dead cycle , It belongs to the ring problem , It needs to be solved with closely related knowledge points, fast and slow pointers . Keep the fast and slow pointer on the ring / The sensitivity of the dead cycle problem .
One 、 Happy number

Two 、 Speed pointer
package everyday.doublePoint;
// Happy number
public class IsHappy {
/* target: Change one number into another , Follow a fixed path to change , Change to 1 Is the number of happy , Entering the dead circle is non block music number . This is similar to a path that may have rings , May be acyclic . The key is to determine whether there is a ring , Acyclic is happy number , If there is a ring, it is a non happy number . The most intuitive idea is hash Record . But for the ring problem , The knowledge point closely related to it is the speed pointer , If the speed pointer collides , There is a ring , Otherwise, the fast pointer goes to the end . */
public boolean isHappy(int n) {
// Initialize the speed pointer position , Fast ahead , Slow again .
int fast = getNext(n), slow = n;
// Quickly and slowly start looking .
while (fast != 1 && fast != slow) {
// fast Take two steps ,slow Take a step , If there are rings, they will collide , otherwise fast Come to the end 1.
fast = getNext(getNext(fast));
slow = getNext(slow);
}
// Look, it's because the ring ends , Or because fast Go to the end .
return 1 == fast;
}
private int getNext(int n) {
int sum = 0;
while (n > 0) {
int mod = n % 10;
sum += mod * mod;
// to update n
n /= 10;
}
return sum;
}
}
summary
1) Keep the fast and slow pointer on the ring / The sensitivity of the dead cycle problem .
reference
边栏推荐
- Arlo's thinking after confusion
- Preparation of functional test report
- Origin2018安装教程「建议收藏」
- rxjs Observable of 操作符的单步调试分析
- Digital currency: far-reaching innovation
- Share some feelings of a programmer who has experienced layoffs twice a year
- # CutefishOS系统~
- MySQL MHA high availability configuration and failover
- 陈天奇的机器学习编译课(免费)
- MySQL中对于事务的理解
猜你喜欢

Understanding of transactions in MySQL

Understanding of indexes in MySQL

Digital currency: far-reaching innovation

Kubernetes创建Service访问Pod

ESP自动下载电路设计

Congratulations on the release of friends' new book (send welfare)

# CutefishOS系统~

Easyexcel complex data export

MySQL -- index of MyISAM storage engine

【无标题】
随机推荐
Hide the creation and use of users
Operation category read is not supported in state standby
Ffmpeg learning notes
LC501. Mode in binary search tree
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
What class loading mechanisms does the JVM have?
数字货币:影响深远的创新
Tourism Management System
SAP 智能机器人流程自动化(iRPA)解决方案分享
业务可视化-让你的流程图'Run'起来
Slope compensation
Quantifiers of regular series
元宇宙可能成为互联网发展的新方向
Flink SQL command line connection yarn
Talk about what parameters ZABBIX monitors
【日常训练】66. 加一
Pytorch nn. functional. Simple understanding and usage of unfold()
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
MySQL view exercise