当前位置:网站首页>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
边栏推荐
- SAP intelligent robot process automation (IRPA) solution sharing
- cvpr2022 human pose estiamtion
- redis配置文件中常用配置详解[通俗易懂]
- MySQL stored procedure
- 搜狗微信APP逆向(二)so层
- Turn -- the underlying debugging principle of GDB is so simple
- mixconv代码
- Wechat open platform scanning code login [easy to understand]
- Cutefishos system~
- nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
猜你喜欢
随机推荐
Slope compensation
分享一个一年经历两次裁员的程序员的一些感触
MySQL view exercise
Appium自动化测试基础 — APPium安装(一)
Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
Flink SQL command line connection yarn
好友新书发布,祝贺(送福利)
Niuke monthly race - logarithmic sum in groups
el-input文本域字数限制,超过显示变红并禁止输入
Fully annotated SSM framework construction
Daily question brushing record (10)
I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
Turn -- the underlying debugging principle of GDB is so simple
人体姿态估计的热图变成坐标点的两种方案
Map container
MySQL MHA high availability configuration and failover
Appium automation test foundation - appium installation (I)
Single step debugging analysis of rxjs observable of operator
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
Sogou wechat app reverse (II) so layer









