当前位置:网站首页>[leetcode 202] happy number
[leetcode 202] happy number
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
Happy number Defined as :
- For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions .
- Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1.
- If this process The result is 1, So this number is the happy number .
If n yes Happy number Just go back to true ; No , Then return to false .
Example 1
Input :n = 19
Output :true
explain :
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2
Input :n = 2
Output :false
I know you're too lazy to calculate and draw ,“ Uncle ” Spoil you . explain
Through the above figure and example 1 You may have a way of thinking , You can do it yourself first . Let's look at the following problem-solving ideas .
Their thinking
recursive
You can see that from the figure above , If it's not happy numbers , after n Time calculation , It is bound to enter a dead circle , That is, you will get the number repeatedly calculated before ( utilize Hashtable Solve this problem ). The number of happiness is calculated a finite number of times 1. Use these two conditions as the conditions for the end of recursion , You can write recursion very quickly .
class Solution {
public:
unordered_set<int> ans;// Use the hash table to determine whether the current value has been calculated
bool isHappy(int n) {
int x=0;
while(n){
x+=pow(n%10,2);
n=n/10;
}
if(x==1)// It's a happy number
return true;
else{
if(ans.count(x)==1)// This number has been calculated before , Not happy numbers
return false;
else{
ans.insert(x);
return isHappy(x);
}
}
}
};
Maybe some students will think of violence to solve , But we don't know what the maximum number of dead cycles will be , Unable to determine the number of cycles of the outer cycle . Personally, I don't recommend doing this , We need to know what a dead cycle stands for , Is to repeat the value calculated before . Use this as the recursive end condition , You can write recursion very quickly .
Welcome to exchange and supplement ~
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012217.html
边栏推荐
- Development environment EAS login license modification
- C language file operation
- Problems and solutions of database migration
- Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
- ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
- Common programming records - parser = argparse ArgumentParser()
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- Mysql database foundation
- 线性代数第一章-行列式
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
猜你喜欢
Implementation of displaying database pictures to browser tables based on thymeleaf
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
检测技术与原理
Complete example demonstration of creating table to page - joint table query
Automatic control (Han min version)
Filebrowser realizes private network disk
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
The user name and password of users in the domain accessing the samba server outside the domain are wrong
随机推荐
Addition, deletion, modification and query of MySQL table
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Common sense of thread pool
The bottom implementation principle of thread - static agent mode
Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
Complete example demonstration of creating table to page - joint table query
Advanced operation of idea debug
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
自动控制(韩敏版)
C language file operation
A sharp tool to improve work efficiency
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
线代第四章-向量组的线性相关
Understanding and installing MySQL
線性代數第二章-矩陣及其運算
Integers have friends interval GCD + double pointer
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
MySQL basic madness theory
Programming training