当前位置:网站首页>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
边栏推荐
- [jetcache] how to use jetcache
- 业务可视化-让你的流程图'Run'起来
- Origin2018安装教程「建议收藏」
- 搜狗微信APP逆向(二)so层
- Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
- The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
- 【日常训练】326. 3 的幂
- Flink SQL command line connection yarn
- 每日刷题记录 (十)
- 3DE resources have nothing or nothing wrong
猜你喜欢

internal field separator

MySQL5.7 设置密码策略(等保三级密码改造)

Favorite transaction code management tool in SAP GUI

447-哔哩哔哩面经1

内部字段分隔符

Cutefishos system~

Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle

Today's sleep quality record 71 points

SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time

SAP intelligent robot process automation (IRPA) solution sharing
随机推荐
Understanding of indexes in MySQL
下班前几分钟,我弄清了v-model与.sync的区别
[untitled]
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
小红书Scheme跳转到指定页面
业务可视化-让你的流程图'Run'起来
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Explain kubernetes network model in detail
[daily training] 66 add one-tenth
The fixed assets management subsystem reports are divided into what categories and which accounts are included
Cut noodles C language
人体姿态估计的热图变成坐标点的两种方案
好友新书发布,祝贺(送福利)
Kubernetes创建Service访问Pod
Lc669. Prune binary search tree
rxjs Observable of 操作符的单步调试分析
How to write a performance test plan
隐藏用户的创建和使用
3DE resources have nothing or nothing wrong
Origin2018 installation tutorial "recommended collection"