Data Structures and Algorithms series of eight (recursive gift)

1. primer

1.1. Why learn data structures and algorithms?

Some people say, data structures and algorithms, computer networks, and operating systems are the same, from the daily development, in addition to the interview in this life probably less than it!

Some people say that I do business development, as long as skilled API, framework skilled, skilled variety of middleware, write the code can not "fly" up?

So the question is: why learning data structures and algorithms it?

# One reason:
    When the interview, do not be data structures and algorithms drag
# Reasons:
    Do you really want to do it forever CRUD Boy
# Three reasons:
    I do not want to write open source frameworks, middleware engineer, not a good cook

1.2 How systematic learning data structures and algorithms?

I thought well, still need to study the data structures and algorithms. But I have two confused:

1. Learn how to go about it?

2. What do you want to learn?

Learning Recommended:

Learning #
 1 . From the ground up, a systematic study
 2 more hands-on, each type of data structures and algorithms are to achieve their own out in code.
 3 ideas is more important: to achieve ideological understanding, do not turn the code
 4 . Combined with the daily development corresponding application scenarios

Learning content recommendation:

Data Structures and Algorithms more content, we in the practical principles, learning classical, commonly used data structures and algorithms commonly used

# Learning content:
 1 definition data structure
 2 . Algorithm defined
 3 . Complexity analysis
 4 common data structures.
    Arrays, linked lists, stacks, queues
    Hash tables, binary trees, heap
    Jump tables, graphs
5 commonly used algorithm
    Recursion, sorting, binary search
    Search, hash, greedy, divide and conquer
    Dynamic programming, string matching

2. Test your knowledge

So far, based on the data structure of the linear form we have read, a brief review, they are: arrays, linked lists, stacks, queues. This data structure is the basis of other data structures and algorithms, need to focus on.

This is a start, we turn train the algorithm, please fasten your seat belts! The first depends on the algorithm is: recursion . Recursive word you must be very familiar with, is there?

If not, let's give an example. From 2016 to the present, the development of knowledge paid in full swing. If you are one of them, for example in the purchase of xx xx platform courses. Most platforms will tell you, will you buy a share of the course out, if someone purchased the course through your shared link, then the platform will give you cash back commission.

Since a relationship with money, it is too much trouble! For the platform, there are several issues that need to figure out. For example: 1. Who is the one person recommended?

2. Who is the second referee ......?

3. Who is the ultimate referee?

Because different level of referees, the proportion of cash back commission not the same, do not back the wrong, right. This is similar to seeking recommendations about people's problems, please have our today's hero debut, it is this: recursion .

# Recursive little more complicated, we learn by two:
 1 . The first chapter is a gift:
   1.1 . Experience life in two small cases
  
2 . The second is the highlight:
   2.1 . Detailed analysis of recursive implementation
   2.2 Notes recursive implementation.

3. Case

3.1. Recommended final demand

Brief:

1. A in certain knowledge payment platform to purchase the course: xx . Share and link to a micro-channel circle of friends

2. B by A link to share, to buy the course: xx . And share the link to a micro-channel circle of friends

3. C via B links to share, to buy the course: xx . And connecting micro-channels share a circle of friends

4. so go ......

5. If at C as a starting point, how to find courses: xx final referees?

6 assumed that the data stored in the database is such that:

 

 

Solving:

1. You must think that this problem is simple good, like this often write the following code:

/**
* Recommended final demand
* / 
Public String findRootRecommend (String userId, String xx) {
     // based on user id to purchase curriculum, curriculum query the database to get recommendations on the user id 
    String [user id] = select Share Share [user id] from [buy] where [curriculum userId user id] = [] and [id] = courses [xx];
    
    // determine whether the share purchase program in accordance friends 
    IF (== share user the above mentioned id null ) {
         return userId;
    }
    
    // 递归查找
    return findRootRecommend(分享用户id,xx);
}

3.2.电影院看电影

简述:

1.你与女朋友正在电影院看电影,电影已经放映

2.突然,女朋友问你:我们坐在电影院的第几排?

3.你一看,坏了:电影院一片漆黑,伸手不见五指

4.这个问题必须要回答,因为是女朋友问的,你该怎么办?

求解:

1.别忘了,你是程序员,对于程序员来说,这个问题太简单了

2.用递归:先问前一排的人,他们在第几排?

3.前一排的人,再问他的前一排,在第几排?

4.以此类推......

5.一直问到第一排的人,第一排不需要再问了,直接回答在第一排

6.第二排的人:在第一排的人基础上 + 1

7.以此类推......

8.每一排都在前一排的基础上 + 1,最后到了你们这一排,女朋友得到了满意的答案

9.你很骄傲有没有?用代码回答,类似这样:

public int movies(int n){
    // 如果是第一排,返回1
    if(n == 1){ return 1;}
    
    // 递归向前一排询问
    return movies(n - 1) + 1;
}

 

Guess you like

Origin www.cnblogs.com/itall/p/12408767.html