Programmer programming Art 1 ~ Chapter 22 highlights and summary (teach you how to program)

Source: Internet
Author: User
Document directory
  • Programmer programming Art 1 ~ Chapter 22 highlights and summary (teach you how to program)
  • Coming soon
Programmer programming Art 1 ~ Chapter 22 highlights and summary (teach you how to program)

 

Author: July, programming art room.
Source:Http://blog.csdn.net/v_JULY_v.

Question

I haven't updated my blog for a long time. Although I only have one month, it has been a long time for me. The most important thing is that, when I came to Beijing in early November, I had to interview, visit, buy books, and read a job in the last month. I haven't typed the code for a long time, and my hands are unfamiliar, although it is only one month, it is still a long time for me. In order to show that I have been paying attention to this blog, I have never left, and I want to be able to think more, but also to keep my hands updated.

OK, for the answer to Microsoft's 100 question and one question, the programmer's programming art has been written into chapter 22nd (art is a high hat that has long been intentionally picked, but it has been worn for so long, still worn out). Some of the answers to these questions are highly debatable. Furthermore, the general summary and details of these articles and questions are listed to facilitate readers to read and review, adapting to the reader's needs can also help you think better.

For the conclusion of the first ten chapters, see this: programmer programming art first ~ Chapter 10 highlights and summary. Below is a summary of the first 22 chapters of the programming art that have been written. If the answers to these dozens of questions have any loopholes or omissions that lead to errors, the readers may find that, I am very grateful to you for your comments and corrections. Thank you.

Selfless sharing for the benefit of the world
  • Chapter 1: Left-rotated string
  • Chapter 2: String inclusion
  • Chapter 3: finding the minimum k Number
  • Chapter 3 continued: Implementation of Top K algorithm problems
  • Chapter 3 continued: in-depth analysis and implementation of quick SELECT Algorithm
  • 3. Continue and find the K small (large) element in the given subscript range in the array.
  • Chapter 4: Compile functions similar to strstr/strcpy/strpbrk
  • Chapter 5: Finding two or more matching conditions
  • Chapter 6. Solving affinity numbers less than 5 million
  • Chapter 7: finding the largest sum of continuous subarrays
  • Chapter 8. Starting from the beginning to the end
  • Chapter 9 catch-up with gossip linked lists
  • Chapter 10: how to sort disk files with 10 ^ 7 data volumes
  • Chapter 1 Longest Common subsequence (LCS)
  • 12th ~ Chapter 15: Number judgment, signing probability, IP address access times, and retrieval questions (First Draft)
  • 16th ~ Chapter 2: Full sorting, step jumping, parity sorting, and the first occurrence only once
  • 21st ~ Chapter 22: generate the shortest abstract with more than half of the number of occurrences
  • Chapter 2 and Chapter 4: Yang's matrix search and inverted index keyword Hash without repeated encoding practices
  • Chapter 2 Jon Bentley: 25th incorrect Binary Search
  • Chapter 2: code and practices for generating inverted indexes based on a given document

Programmer programming Art 1 ~ Chapter 22: Below is a rough summary of each chapter. For details, please refer to the original article at the link:

  • Chapter 1: Left-rotated string

Original Title: Define the left rotation operation of the string: Move several characters before the string to the end of the string.
For example, the string abcdef is rotated two places to the left to obtain the string cdefab.
Please implement the left rotation function of the string. the time complexity for string operations with the length of n is O (n), and the space complexity is O (1 ).

The original article provides the following ideas:

  1. Here is an example of abcdef:
    1,There are two parts: X: abc, Y: def;
    2,X-> X ^ T, abc-> CBA, Y-> Y ^ T, def-> fed.
    3,(X ^ TY ^ T) ^ T = YX, cbafed-> defabc, that is, the entire flip.
  2. The two pointers are flipped gradually after the first one;
  3. Through recursive conversion, the problem scale is reduced;
  4. Stl: rotate algorithm;

After searching for this left-rotated string, we found that the first option of the search engine was mostly linked to other websites that reprinted this article, not original or original, it indicates reprinting and piracy. Therefore, no search results are provided.

 

  • Chapter 2: String inclusion

Original Title: Assume that there is a character string consisting of letters. Assume that there is another character string, and the number of letters in the string is relatively small. In terms of algorithms, how can we quickly find all the letters in a small string in a large string?

For example, if it is the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOM
The answer is true, and all the letters in string2 also have string1.

For the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOZ
The answer is false because the Z letter in the second string is not in the first string.

 

  • Chapter 3: finding the minimum k Number

Original Title: 5. Find the minimum k Elements
Question: Enter n integers and output the smallest k integers.
For example, if you enter the 8 numbers 1, 2, 3, 5, 6, 7, and 8, the minimum four digits are 1, 2, 3, and 4.

 

  • Chapter 3 continued: Implementation of Top K algorithm problems

Original Title: the search engine will record all the search strings used by the user each time through log files. The length of each query string is 1-bytes.
Suppose there are currently 10 million records (these query strings have a relatively high degree of repetition, although the total number is 10 million, but if the repetition is not removed, there will be no more than 3 million records. The higher the repetition of a query string, the more users query it, that is, the more popular it is .), Please count the top 10 query strings. The memory required cannot exceed 1 GB.

 

  • Chapter 3 continued: in-depth analysis and implementation of quick SELECT Algorithm

"OK, I propose an algorithm in Chapter 3, that is, to quickly SELECT the SELECT algorithm. This SELECT algorithm selects the median in the array as the pivot element to ensure that in the worst case, it can also prove the time complexity of linear O (N), which has also been given in Chapter 3 of fantasy."

 

  • 3. Continue and find the K small (large) element in the given subscript range in the array.

"Companion Array

After the first sorting:

A [I]. data 1 2 3 4 5 6 7
A [I]. num 1 3 5 7 2 4 6

Run O (N) to scan again:

A [I]. data 1 2 3 4 567
A [I]. num 1 3 5 7 2 4 6
K 3 2 1 1 1 0

(In the past, some readers did not realize the meaning of the accompanying array because the average user only looks for it once and does not think of it for the second or multiple searches )"

 

  • Chapter 4: Compile functions similar to strstr/strcpy/strpbrk

"Many small and medium-sized companies have limited creative abilities, including limited human and material resources. Therefore, apart from copying the questions of some large companies (of course, you will certainly not be able to evaluate your knowledge of the basic knowledge. Another way is to allow you to within a limited period of time (such as 10 minutes ), implement some library functions such as strcpy, strcat, and strpbrk on the spot. This mainly depends on your understanding of the details and whether your programming capabilities are solid."

 

  • Chapter 5: Finding two or more matching conditions

Original Title:

  1. Enter an array and a number, and search for two numbers in the array so that their sum is exactly the number entered.
    The time complexity is O (n ). If there are multiple pairs of numbers and the sum is equal to the input number, output any one.
    For example, input arrays 1, 2, 4, 7, 11, 15, and numbers 15. Because 4 + 11 = 15, 4 and 11 are output.
  2. Enter two integers, n and m, and random numbers from the series 1, 2, 3 ...... n,
    To make the sum equal to m, all possible combinations must be listed.

 

  • Chapter 6. Solving affinity numbers less than 5 million

Original Title: calculate the number of all affinity values within 5 million
If the sum of all true factors of two numbers a and B and a is equal to B, and the sum of all true factors of B is equal to a, a and B are a pair of affinity numbers.
For example 220 and 2924 and.

 

  • Chapter 7: finding the largest sum of continuous subarrays

Original Title: enter an integer array. The array contains positive and negative numbers.
One or more consecutive integers in the array form a sub-array. Each sub-array has a sum.
Returns the maximum value of the sum of all sub-arrays. The time complexity is O (n ).

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the maximum sub-array is 3, 10,-4, 7, 2, so the output is the sum of 18 of the Child array.

 

  • Chapter 8. Starting from the beginning to the end

 

  • Chapter 9 catch-up with gossip linked lists

 

  • Chapter 10: how to sort disk files with 10 ^ 7 data volumes

Original question: input: a file that contains up to n non-repeating positive integers (that is, it may contain less than n non-repeating positive integers), each of which hasLess than or equalN, and n = 10 ^ 7.
Output: Lists All input integers in ascending order.
Condition: a maximum of 1 MB of memory is available, but the disk space is sufficient. The running time must be less than 5 minutes, and 10 seconds is the best result.

 

  • Chapter 1 Longest Common subsequence (LCS)

"What is the longest common subsequence? Like a seriesSIf they are subsequences of two or more known sequences and are the longest of all the sequences that meet the conditionSThe longest common subsequence of a known sequence. For example, if there are two random sequences, such as 1 3 4 5 5, and 2 4 5 5 7 6, their longest common subsequences are: 4 5 5 ."

 

  • 12th ~ Chapter 15: Number judgment, signing probability, IP address access times, and retrieval questions (First Draft)

This article consists of the following four parts:

  1. The first part is about the data structure, data compression, and bitmap algorithms from a single question,
  2. Part 2: traverse n elements and retrieve one of them at random by probability, which is completed by BigPotato,
  3. Part 3: extract the IP address with the most visits to Baidu on a day, which is completed by luuillu,
  4. Part 4: Return judgment, completed by well. The full text is completed by the July system.

 

  • 16th ~ Chapter 2: Full sorting, step jumping, parity sorting, and the first occurrence only once

This chapter describes the following questions:

  1. Full Arrangement;
  2. Step jumping;
  3. Parity sorting;
  4. The first character that appears only once;
  5. Consistent hash algorithm.

 

  • 21st ~ Chapter 22: generate the shortest abstract with more than half of the number of occurrences

This article describes two problems:

  1. An array contains a number that is more than half the length of the array.
  2. You can click the first four words "Structure Method" in the search box of Baidu or Google to view the blog link in the first option, as shown in Figure 2:
Figure 2 Google search keyword "structure method"
In Figure 2 shown above, the search result is "structure-based algorithm-blog channel-CSDN. NET "has a descriptive text:" Programmer interview, Algorithm Research, programming art, red and black tree 4 classic original series collection and summary by: July -- Structure algorithm... this text is called the abstract of the search result, that is, the shortest abstract. Our question is, how is the shortest digest generated?

First ~ Chapter 22.

Coming soon

The following code may be used as a part of the subsequent chapter of the programming Art series:

1. merge two sorted linked lists.That is, two linked lists are input in ascending order. The two linked lists are merged and the nodes in the new linked list are still sorted in ascending order.

// Merge two sorted linked lists
// Copyright @ 2011 offoffoffoffoffer
ListNode * Merge (ListNode * pHead1, ListNode * pHead2)
{
If (pHead1 = NULL)
Return pHead2;
Else if (pHead = NULL)
Return pHead1;

ListNode * pMergedHead = NULL;

If (pHead1-> m_nValue <pHead-> m_nValue)
{
PMergedHead = pHead1;
PMergedHead-> m_pNext = Merge (pHead1-> m_pNext, pHead2 );
}
Else
{
PMergedHead = pHead2;
PMergedHead-> m_pNext = Merge (pHead1, pHead2-> m_pNext );
}

Return pMergedHead;
}

// Non-recursive sorting and merging of linked lists
// Copy @ 2011 July
Node * Merge (Node * head1, Node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
Node * p1 = NULL;
Node * p2 = NULL;

If (head1-> data {
Head = head1;
P1 = head1-> next;
P2 = head2;
}
Else
{
Head = head2;
P2 = head2-> next;
P1 = head1;
}

Node * pcurrent = head;
While (p1! = NULL & p2! = NULL)
{
If (p1-> data <= p2-> data)
{
Pcurrent-> next = p1;
Pcurrent = p1;
P1 = p1-> next;
}
Else
{
Pcurrent-> next = p2;
Pcurrent = p2;
P2 = p2-> next;
}
}

If (p1! = NULL)
Pcurrent-> next = p1;
If (p2! = NULL)
Pcurrent-> next = p2;

Return head;
}
// I haven't knocked on the code for a long time. It feels so comfortable to knock on the code...

2. Reverse linked list

// Question 24th: reverse linked list
// PPrev <-pNode <-pNext
// Copyright @ He Haitao Harry
ListNode * ReverseIteratively (ListNode * pHead)
{
ListNode * pReversedHead = NULL;
ListNode * pNode = pHead;
ListNode * pPrev = NULL;
While (pNode! = NULL) // pNode <=> m
{
ListNode * pNext = pNode-> m_pNext; // n is saved under pNext

// If pNext is null, the current node pNode is set as the header.
If (pNext = NULL)
PReversedHead = pNode;

// Reverse the linkage between nodes
PNode-> m_pNext = pPrev;

// Move forward on the list
PPrev = pNode;
PNode = pNext;
}
Return pReversedHead;
}

// Or, write as follows:
// Head-> next-> p-> q
Template <class T>
Node <T> * Reverse (Node <T> * head)
{
P = head-> next;

While (p)
{
Q = p-> next; // p-> next is saved under q
P-> next = head-> next; // p turns to head-> next
Head-> next = p; // p is assigned to head-> next
P = q; // q is assigned to p.
// The above two rows are the pointer moving forward...
}

Return head;
}

As I have already said before, the "programming Art series may end with Chapter 1 ~ Chapter 10 highlights and summaries-interview, algorithm, and programming ). So, To be continued ....

If you cannot understand or understand a specific question or answer it well for the moment, it is because you think and receive questions differently from me. But don't worry. You can understand or understand it in your own way, but it takes some time. Good luck!

 

Postscript

Before coming to Beijing, I made the CHM file in the fifth part of this blog post, but at this point.

In fact, the solution is very simple. simply remove the check box "always ask (W) before opening the file" in the prompt box when opening the file. For this reason, the following is an example.

1. After downloading the file, click the file and remove the check box "always ask (W) before opening the file" in the following prompt box:

2. Open it directly, as shown in:

3. reader feedback: If it is vista or another system, you can right-click and modify the attribute (if the directory is displayed at the beginning, there is an "unlock" in the attribute. Click it to open it ).

So, you may wish to try again yourself? For this file: http://download.csdn.net/detail/v_july_v/3738298. Thank you. I hope this document will be helpful for your interview, programming or algorithm learning. Other resources are as follows:

  • Microsoft and other data structures + algorithm interview 100 questions all answer highlights: http://download.csdn.net/detail/v_july_v/4105306;
  • Thirteen classical algorithm research PDF documents [with directory + tag]: http://download.csdn.net/detail/v_JULY_v/3427838;
  • [Collector version] Microsoft and other data structures + algorithm interview 100 questions are all published [100 question V0.1 final perfect version]: http://download.csdn.net/detail/v_JULY_v/2885434;
  • All resources:Http://download.csdn.net/user/v_JULY_v.

At the same time, my friend is helping to organize all the articles in my blog into PDF documents with directories and tags. After that, I will share them with you.

 

Some people often ask me how to learn algorithms. To be honest, I used to write a blog at school. I learned algorithms only on the eve of my graduation, and I had no class. I was just idle. When a person is bored, he has to find something to do. For example, if most people choose to play games, playing cards is also a pastime, with no pursuit or purpose. If you are not satisfied yet, you can refer to this article: This article is derived from quick sorting-how to learn algorithms. Learning algorithms is a matter of unique interest and passion (of course, a certain language and data structure foundation are also essential); learning programming, you should think more about code.

Many of my friends asked me to recommend some basic books and materials on programming or algorithm learning. Here, we recommend the following ten items: 1. c ++ primer; 2. Introduction to algorithms; 3. c and pointers; 4. STL source code analysis; 5. Deep Exploration of the c ++ object model; 6. Programming; 7. The beauty of programming; 8. in-depth understanding of computer systems; 9. programmer programming art (may be renamed later); 10. Structure-based algorithm blog.

As for work, I am looking for a suitable development platform and a suitable position to work hard. From now on, the future updates of this blog may be different from the existing content. For more information, let me look forward to it.

 

OK. Do not talk too much. If a reader can only read the original question given in this article and immediately reflect the ideas and solutions of each question, the interview will be free of worries. Please contact me to discuss, exchange, and consult with you. Of course, if other Readers discover the vulnerabilities, omissions, or errors of any question in this series of articles, please give me some advice or criticize and correct them. You can contact me through the following channels:

  1. Algorithm exchange group 16th: Algorithms_16, 30382647 (valid within November );
  2. My weibo blog (left sidebar): @ v_JULY_v, http://weibo.com/julyweibo, my mailbox, zhoulei0907@yahoo.cn.
  3. Renren algorithm team, http://xiaozu.renren.com/xiaozu/245753? Feed;
  4. TAOPP revision wiki, http://tctop.wikispaces.com /;
  5. At present, I am building a forum with a friend to serve as a place for a group of friends. When I think of it, I can find my own paradise, whether it's for irrigation or technology.

We are proud to share our learning and communication with you with a humble heart. . July and July November 23, 2011.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.