当前位置:网站首页>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
边栏推荐
猜你喜欢

Sogou wechat app reverse (II) so layer

How to write a performance test plan

104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目

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

今日睡眠质量记录71分

map容器

轉載csdn文章操作

搜狗微信APP逆向(二)so层

Selection of all-optical technology in the park - Part 2

Flynk SQL client uses comparison and is familiar with official documents
随机推荐
[target tracking] | single target tracking indicator
Use and function of spark analyze command map join broadcast join
Appium自动化测试基础 — APPium安装(一)
Using securecrtportable to remotely connect virtual machines
SAP 智能机器人流程自动化(iRPA)解决方案分享
阿洛迷茫后的思考
转--拿来即用:分享一个检查内存泄漏的小工具
MySQL中对于索引的理解
QStringList 的常规使用
map容器
Turn -- the underlying debugging principle of GDB is so simple
Understanding of transactions in MySQL
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
轉載csdn文章操作
陈天奇的机器学习编译课(免费)
小红书Scheme跳转到指定页面
internal field separator
Electron学习(三)之简单交互操作
转--原来gdb的底层调试原理这么简单
Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant