当前位置:网站首页>leetcode-202.快乐数
leetcode-202.快乐数
2022-07-26 03:14:00 【KGundam】
数学问题
题目详情
编写一个算法来判断一个数 n 是不是快乐数。
「快乐数」 定义为:
对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
如果这个过程 结果为 1,那么这个数就是快乐数。
如果 n 是 快乐数 就返回 true ;不是,则返回 false 。
示例1:
输入:n = 19
输出:true
解释:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
示例2:
输入:n = 2
输出:false
思路:
这道题首先想到的方法就是循环求各数位平方和,利用集合存储,循环中如果找到1就返回true,找到存过的平方和就返回false
我的代码:
class Solution
{
public:
// 辅函数 求各个数位平方和
int getSum(int n)
{
int sum = 0;
while (n)
{
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
// 主函数
bool isHappy(int n)
{
int sum = 0;
unordered_set<int> set;
while (true)
{
sum = getSum(n);
if (sum == 1) return true; // 循环找到1就返回true
// 循环找到重复就返回false
if (set.find(sum) != set.end()) return false;
//否则存入sum后更新n继续循环
set.insert(sum);
n = sum;
}
}
};
在写第一种方法的时候我们可以发现,我们其实是在一个循环中不断寻找指定元素,我们可以联想到在循环链表中的寻找元素的一种好方法:快慢指针法!
即定义慢指针slow慢指针初始在第一个节点上,快指针fast初始在第二个节点上,然后slow每次走一步,fast每次走两步,直到fast找到1(true)或者fast和slow相遇(没找到死循环false) 代码如下:
class Solution
{
public:
// 辅函数 求各个数位平方和
int getSum(int n)
{
int sum = 0;
while (n)
{
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
// 主函数
bool isHappy(int n)
{
//初始化
int slow = n;
int fast = getSum(n);
// 没找到1 不相遇 就循环
while (fast != 1 && slow != fast)
{
slow = getSum(slow);//执行一步
fast = getSum(getSum(fast));//执行两步
}
return fast == 1; //最后跳出循环看fast是不是1
}
};
最后还有一种方法可以通过数学规律来解决,当一个数的各位平方和最后无法循环成1,那么必定会掉入循环:4→16→37→58→89→145→42→20→4
根据这个规律我们可以写出下面代码:
class Solution
{
public:
// 辅函数 求各个数位平方和
int getSum(int n)
{
int sum = 0;
while (n)
{
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
unordered_set<int> set{
4, 16, 37, 58, 89, 145, 42, 20};//初始化
// 主函数
bool isHappy(int n)
{
// 没找到1 set中没找到(没掉入死循环)
while (n != 1 && set.find(n) == set.end())
{
n = getSum(n); //就继续
}
return n == 1;
}
};
边栏推荐
- Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘
- QT notes - temporary floating window
- Execution process behind shell commands
- [noip2001 popularization group] packing problem
- Canvas -- draw text -- modification of pie chart
- Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
- DDD落地的那叫一个高级
- 使用VRRP技术实现网关设备冗余,附详细配置实验
- 如何正确计算 Kubernetes 容器 CPU 使用率
- There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
猜你喜欢

LoRa无线网关如何快速实现端到云的传输

Managing databases in a hybrid cloud: eight key considerations

在混合云中管理数据库:八个关键注意事项

LoRa和NB-IOT可用用在哪些地方

HCIP第十四天

Why did Mr. Cao, a productionist in the field of high-end tea, choose Ruifeng L6 max?

"Xiao Deng's view" the value brought by Siem to enterprises (II)

Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)

【无标题】

Completion report of communication software development and Application
随机推荐
Canvas -- draw curve -- wall clock, pie chart, five pointed star
easyExcel设置行隐藏,解决setHidden(true)失效问题
图解LeetCode——5. 最长回文子串(难度:中等)
Win11 method of changing disk drive letter
Course notes of single chip microcomputer principle and interface technology for migrant workers majoring in electronic information engineering
使用anaconda配置gpu版本的tensorflow(30系列以下显卡)
爆肝出了4W字的Redis面试教程
Managing databases in a hybrid cloud: eight key considerations
C语言预处理指令以及Makefile脚本讲解
Classic interview questions -- three characteristics of OOP language
2022-07-21 第四小组 修身课 学习笔记(every day)
如何正确计算 Kubernetes 容器 CPU 使用率
Use Anaconda to configure the tensorflow of GPU Version (less than 30 series graphics cards)
Opencv annotates the image (picture frame + writing)
线性回归原理推导
Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
ELS message loop
复制列表时踩过的坑:浅拷贝与深拷贝
URDF 语法详解
LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS