当前位置:网站首页>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
边栏推荐
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- Quantifiers of regular series
- Electron学习(三)之简单交互操作
- Operation category read is not supported in state standby
- Digital currency: far-reaching innovation
- Pytorch nn. functional. Simple understanding and usage of unfold()
- Metauniverse may become a new direction of Internet development
- 【日常训练】326. 3 的幂
- Using securecrtportable to remotely connect virtual machines
- Cloud Vulnerability Global Database
猜你喜欢
rxjs Observable of 操作符的单步调试分析
map容器
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
Explain kubernetes network model in detail
激发新动能 多地发力数字经济
[jetcache] how to use jetcache
El input text field word limit, beyond which the display turns red and input is prohibited
[untitled]
【无标题】
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
随机推荐
Mysql database detailed learning tutorial
redis配置文件中常用配置详解[通俗易懂]
小红书Scheme跳转到指定页面
How to write a performance test plan
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
Reprint CSDN article operation
Réimpression de l'article csdn
Deep learning -- data operation
Kubernetes创建Service访问Pod
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
"Trust machine" empowers development
激发新动能 多地发力数字经济
[target tracking] | single target tracking indicator
效率提升 - 鼓捣个性化容器开发环境
Easyexcel complex data export
聊一聊Zabbix都监控哪些参数
Flynk SQL client uses comparison and is familiar with official documents
Metauniverse may become a new direction of Internet development
pytorch训练自己网络后可视化特征图谱的代码
【日常训练】66. 加一