当前位置:网站首页>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
边栏推荐
- LC501. Mode in binary search tree
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- [C language] detailed explanation of malloc function [easy to understand]
- 使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
- What class loading mechanisms does the JVM have?
- The fixed assets management subsystem reports are divided into what categories and which accounts are included
- Mysql5.7 set password policy (etc. three-level password transformation)
- Understanding of inverted residuals
- A few minutes before work, I found out V-model and The difference between sync
猜你喜欢

Appium automated testing foundation - Supplement: introduction to desired capabilities parameters

Appium自动化测试基础 — APPium安装(一)

FFMpeg学习笔记

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

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

转--拿来即用:分享一个检查内存泄漏的小工具

2020-ViT ICLR

Sogou wechat app reverse (II) so layer

元宇宙可能成为互联网发展的新方向

Turn -- the underlying debugging principle of GDB is so simple
随机推荐
C#/VB. Net to add text / image watermarks to PDF documents
【无标题】
MySQL stored procedure
Turn -- bring it and use it: share a gadget for checking memory leaks
[QT widget] encapsulates a simple thread management class
FFMpeg学习笔记
转--拿来即用:分享一个检查内存泄漏的小工具
mixconv代码
What class loading mechanisms does the JVM have?
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
切面条 C语言
Deep learning -- data operation
[image segmentation] 2021 segformer neurips
LC501. Mode in binary search tree
Fiori 应用通过 Adaptation Project 的增强方式分享
The principle, testing and Countermeasures of malicious software reverse closing EDR
Share some feelings of a programmer who has experienced layoffs twice a year
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
利用SecureCRTPortable远程连接虚拟机