当前位置:网站首页>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
边栏推荐
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- 【日常训练】326. 3 的幂
- 好友新书发布,祝贺(送福利)
- Cloud Vulnerability Global Database
- Pytorch nn.functional.unfold()的简单理解与用法
- How to write a performance test plan
- Hide the creation and use of users
- Tourism Management System
- 恶意软件反向关闭EDR的原理、测试和反制思考
- 447 Bili Bili noodles warp 1
猜你喜欢

切面条 C语言

Slope compensation

Explain kubernetes network model in detail

Fiori 应用通过 Adaptation Project 的增强方式分享

3DE resources have nothing or nothing wrong

447 Bili Bili noodles warp 1

Easyexcel complex data export

FFMpeg学习笔记

Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration

Stimulate new kinetic energy and promote digital economy in multiple places
随机推荐
mixconv代码
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
447 Bili Bili noodles warp 1
Using emqx cloud to realize one machine one secret verification of IOT devices
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
internal field separator
[daily training] 326 Power of 3
Lc669. Prune binary search tree
Ffmpeg learning notes
Resttemplate remote call tool class
利用SecureCRTPortable远程连接虚拟机
【日常训练】326. 3 的幂
redis配置文件中常用配置详解[通俗易懂]
General use of qstringlist
今日睡眠质量记录71分
激发新动能 多地发力数字经济
Efficiency improvement - encourage personalized container development environment
Mysql5.7 set password policy (etc. three-level password transformation)
倒置残差的理解
[image segmentation] 2021 segformer neurips