当前位置:网站首页>Recursive structure
Recursive structure
2022-07-04 03:18:00 【hv102】
Recursive structure consists of two parts :
- Define recursive headers . solve : When not to call your own methods . If there is no head , Will fall into a dead cycle , That is, the end condition of recursion .
- Recursive body . solve : When do I need to call my own methods .
- Example : Use recursion to find n
public class Test22 { public static void main(String[ ] args) { long d1 = System.currentTimeMillis(); factorial(10); long d2 = System.currentTimeMillis(); System.out.printf(" Recursive time-consuming :"+(d2-d1)); // Time consuming :32ms } /** The way to find factorial */ static long factorial(int n){ if(n==1){// Recursive header return 1; }else{// Recursive body return n*factorial(n-1);//n! = n * (n-1)! } } }
The execution result is shown in the figure :
The flaw of recursion
Simple algorithm is one of the advantages of recursion . But recursive calls take up a lot of system stack , Memory consumption , When there are many levels of recursive calls, the speed is much slower than the loop , So be careful when using recursion .
边栏推荐
- Add token validation in swagger
- Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
- Solve the problems encountered by the laravel framework using mongodb
- Jenkins continuous integration environment construction V (Jenkins common construction triggers)
- VRRP+BFD
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- (practice C language every day) pointer sorting problem
- What is the difference between enterprise wechat applet and wechat applet
- Career development direction
- The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
猜你喜欢
If you have just joined a new company, don't be fired because of your mistakes
Development of digital collection trading platform development of digital collection platform
Node write API
Have you entered the workplace since the first 00???
New year's first race, submit bug reward more!
Webhook triggers Jenkins for sonar detection
Dare to climb here, you're not far from prison, reptile reverse actual combat case
Record a problem that soft deletion fails due to warehouse level error
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Leetcode51.n queen
随机推荐
Solve the problems encountered by the laravel framework using mongodb
PHP database connection succeeded, but data cannot be inserted
MySQL data query optimization -- data structure of index
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
Résumé des outils communs et des points techniques de l'examen PMP
Slurm view node configuration information
WordPress collection WordPress hang up collection plug-in
基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
Crawler practice website image batch download
3D game modeling is in full swing. Are you still confused about the future?
1day vulnerability pushback skills practice (3)
Baijia forum the founding of the Eastern Han Dynasty
Enhanced for loop
Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
Kiss number + close contact problem
Site favorites
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
Have you entered the workplace since the first 00???