当前位置:网站首页>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
边栏推荐
- Today's sleep quality record 71 points
- 【日常训练】326. 3 的幂
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- Congratulations on the release of friends' new book (send welfare)
- map容器
- Rust语言——小小白的入门学习05
- nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
- Cut noodles C language
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Rust language - Introduction to Xiaobai 05
猜你喜欢

rxjs Observable of 操作符的单步调试分析

Easyexcel complex data export
![[image segmentation] 2021 segformer neurips](/img/2f/a8631cbe9a46419b8dbd5205e1f5b5.png)
[image segmentation] 2021 segformer neurips

Single step debugging analysis of rxjs observable of operator

激发新动能 多地发力数字经济

Appium自动化测试基础 — 补充:Desired Capabilities参数介绍

Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock

Delete AWS bound credit card account

正则系列之量词(Quantifiers)

"Trust machine" empowers development
随机推荐
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
Pytorch nn. functional. Simple understanding and usage of unfold()
Cloud Vulnerability Global Database
Fully annotated SSM framework construction
阿洛迷茫后的思考
Little red book scheme jumps to the specified page
聊一聊Zabbix都监控哪些参数
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
MySQL中对于索引的理解
Origin2018 installation tutorial "recommended collection"
QStringList 的常规使用
SAP GUI 里的收藏夹事务码管理工具
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
激发新动能 多地发力数字经济
Flink SQL command line connection yarn
Today's sleep quality record 71 points
internal field separator
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
Fiori 应用通过 Adaptation Project 的增强方式分享