当前位置:网站首页>[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
边栏推荐
- Algèbre linéaire chapitre 2 - matrice et son fonctionnement
- Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
- Protected (members modified by protected are visible to this package and its subclasses)
- Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
- Failure to deliver XID in Seata distributed transaction project
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
- ThreadLocal. Threadlocalmap analysis
- Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
- Explain of MySQL optimization
- Create enterprise mailbox account command
猜你喜欢
The bottom implementation principle of thread - static agent mode
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
Guaba and Computational Geometry
Installation and usage skills of idea
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
检测技术与原理
Linear algebra Chapter 1 - determinant
Fundamentals of digital image processing (Gonzalez) I
Filebrowser realizes private network disk
St table template
随机推荐
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
Integration and induction of knowledge points of automatic control principle (Han min version)
Fact final variable and final variable
Addition, deletion, modification and query of MySQL table
Customized communication between threads (reentrantlock)
Solution record of slow access speed of SMB service in redhat6
The attendance client date of K / 3 wise system can only be selected to 2019
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Preparedstatement prevents SQL injection
Linear algebra Chapter 2 - matrices and their operations
5.The Simple Problem
Illustrate the significance of hashcode
線性代數第二章-矩陣及其運算
20 excellent plug-ins recommended by idea
10.Advance Next Round
6.Reversal
Filebrowser realizes private network disk
DBCP usage
In depth source code analysis servlet first program
Stability building best practices