Reading Notes of the beauty of programming (1)

Source: Internet
Author: User

2.1 calculate the number of 1 in the binary number

Http://blog.csdn.net/bvbook/archive/2008/04/15/2292823.aspx

An integer of 8 bytes. Its binary value indicates the number of 1.

Four algorithms:

1) divide the cycle by 2 to determine whether the remainder is 1

2) differs from 0x01 (&) to determine whether the last digit is 1, and then shifts one digit to the right to return and forth.

The time complexity of algorithms 1 and 2 is O (logn)

3) this algorithm is rather hard to erase the last 1, so that the time complexity is only related to the number of 1.

N & (n-1). This technique is very important and can calculate the last position of 1 at the end.

While (V)

{

N = N (n-1)

Num ++;

}

Return num;

4) for 8 bytes, its value is only 256 possibilities, so it is even harder to create an array of int [256], so that these values are prepaid, then, the ARR [I] can take any one of them. The time complexity is O (1 ).

 

In other words, I did not understand the 32-bit algorithm because of my family.

Another extended question is to first make an exception or to a and B, and then calculate the number of 1 in the result value.

C # programmers, remember:

| Calculation rule: 0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0 in simple words, one digit is 1 and 1 is 1.

Do not mix with XOR. The latter is 0 and the difference is 1.

& Operation rule: if the value is 1, it is 1. Otherwise, 0 is returned.

 

2.2 do not be intimidated by factorial

Http://www.kuqin.com/algorithm/20080505/7874.html

The first question, n! How many zeros are there at the end?

In fact, it is calculating the number of five. [N/5] + [N/25] + [N/125] + ......, [N/5] indicates the number of divisible values from 1 to n?

This is a kind of "suede cramps" idea:

Ret = 0

While (N)

{

RET + = N/5;

N/= 5;

}

 

2nd questions, n! In the binary representation?

In fact, it is to calculate the number of two prime factors from 1 to n-there are X 2, so n! There are x 0 at the end of the binary, so the first 1 is n + 1.

[N/2] + [N/4] + [N/8] + ......

 

Extended question: Is n a power of 2?

N & (N-1) = 0, of course, but also ensure that N> 0;

 

2.3 search for the poster

Http://blog.csdn.net/zhong317/archive/2009/07/15/4350690.aspx

This algorithm is very interesting to delete two different IDs each time. For example, if a, A, B, C, A, B, A, and a, then ntimes is first increased to 2, and then, because B and C appeared and dropped to 0, candidate was reset. At last, ntimes will always be greater than 0, and candidate will not change.

The extended question is too complicated. My suggestion is to narrow down the scope to: There are two posts with many IDs, all of which have exceeded 1/3. How to find out? The idea is still to kill three different times, so as to ensure that there are still more than 1/3 IDs.

So we need to create an array: ntimes [0] And ntimes [1]. We need to create the corresponding values candidate [0] and candidate [1]. Let's talk about the process:

For example, if the first three numbers are a, a, and B, candidate [0] = A, candidate [1] = B, ntimes [0] = 2, ntimes [1] = 1, the following question comes,

If the number of 4th is A or B, the number of ntimes [0] And ntimes [1] is increased accordingly.

If the 4th numbers are C, they are different from those of candidate [0] and candidate [1], so we think we have found three different numbers and killed them, the kill process is ntimes [0] And ntimes [1] minus 1. If any of them becomes 0, the corresponding candidate is cleared, here ntimes [1] minus 1 is changed to 0, so candidate [1] is to be cleared, waiting for the next value to come in.

In this way, candidate [0] and candidate [1] are the two water kings we are looking.

 

To expand the question, you need to find three IDs with more than 1/4 posts. You can follow my ideas.

 

From this question, we can see the advantages of divide governance, greed, and recurrence.

 

2.4 "1" Number

Calculate the number of 1 in F (N). This question tells us the importance of enumeration + induction. Generally, it is up to four digits to sum up all the subsequent situations. The number of occurrences of 1 on the nth digit is determined by three factors: the number on the nth digit, the number above the N digit, And the number below the N digit.

The number on the nth digit is discussed in three cases:

1) 0: The N power of x10.

2) to 1: not only determined by the high position: the N power of the higher digit x10; also affected by the low position: the current N-bit and below values + 1

3) greater than 1: (a higher number + 1) The Npower of x10

 

Calculate F (n) = N

I disagree with the solution in the book. It is acceptable to calculate the upper limit of 10 to the power of 11. However, it is very difficult to find N in descending order. I suggest using the bipartite method. O (longn) is much more efficient than O (n.

 

Extended question analysis: Objective binary method

 

2.5 find the maximum K number

Alas, it is also sorting, and it is the first thing to get rid of the knowledge of sorting algorithms. Alas, a person who is not from a division is suffering. If there is an afterlife, I don't even have to make the order for programmers.

 

2.6 precise floating point expression

 

2.7 Max public appointment Problems

Division of the moving phase is not good, and division of the moving phase is not good.

If the value is 0, it is shifted to the right. If the value is 1, it is subtracted until one is 0.

 

2.8 find the qualified integer

 

2.9 fib Series

In fact, I like to use space for time, whether F (n) = f (n-1) + f (n-2), or F (n) = f (n-1) + f (n-2) + f (n-3 ).

Of course, using the N power of the unit matrix is also a good solution.

 

2.10

Find the maximum and minimum number in the array: It seems that the shortest is only 1.5N, who makes the maximum and minimum distance so disparity.

Find the two largest numbers in N Arrays: two groups of orders, the big front (odd digits), as long as the odd digits, in a new array (concept) -- continue to repeat until 4-5 are left.

How do I select the maximum (small) N number from the N number? Http://blog.csdn.net/fisher_jiang/archive/2008/05/23/2473698.aspx

Loser tree)

 

2.11 search for the nearest point

Http://topic.csdn.net/u/20091024/12/989417AA-60E9-45D1-A96F-A623695FC6D7.html

 

2.20 program understanding and Time Analysis

Http://blog.csdn.net/jcwKyl/archive/2009/02/14/3889802.aspx

Note: Other algorithm articles of this blogger are quite good.

 

2.21 add interview questions

Http://blog.csdn.net/haykey/archive/2008/10/29/3175373.aspx

 

3.1 string shifting and Inclusion Problems

If S2 can be obtained from S1 cyclic shift, s2 must be on s1s1.

There seems to be an algorithm called "least representation"

 

3.2 English words for phone numbers

 

3.3 delete a node from a single-chain table with no Headers

Tom for Prince:

Curr. Data = curr. Next. Data;

Curr. Next = curr. Next. Next;

 

3.6

Linked List intersection expansion problem http://hi.baidu.com/azuryy/blog/item/18e85b02ec34a4094bfb51de.html

 

 

 

Single-chain table reversal:

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.