And data manipulation algorithms prove safety offer2.4-

8 title

The beginning of an array of several elements moved to the end of the array, the array we call rotation. 
A non-descending order of the input array of a rotation of the output rotary smallest element array. 
Array {e.g. 3 , 4 , 5 , 1 , 2 } to { 1 , 2 , 3 , 4 , 5 } of a rotation, the array is a minimum. 
NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

Thinking

An array of topics given in to some extent is sorted , so you can use binary search to find the idea that the smallest element.

Binary search is generally set two pointers P1, P2 pointing to the first element and the last element of the array, and then find the middle element, compare the size range of a pointer pointing to shrinking.

We need to find is the critical point increment and decrement array of arrays.

1. The first point, if the intermediate element is larger than the first element, then that element is located in the front half of the intermediate array is incremented. The intermediate element can move the position of point P1

2. If the intermediate element is smaller than the first element, then that element is in the middle of the second half of the array down, the position of the intermediate point P2 of the mobile element.

3. Comparison of the size of the continuous intermediate element pointer P2 and P1 and the elements P1, updates the pointer to point, until the pointer to two adjacent positions. This can be described in two pointer has a critical point of the array. So the point P2 element is the smallest element, P1 is the point of maximum element

4. Note that considering the situation repeated elements appear, did not consider the following answers. That is, when the two numbers, and pointers to their three digital intermediate, the intermediate element can not determine the increasing or decreasing in the array, can not be narrowed down by moving the pointer. It is necessary to complete the order by looking. In this case it needs to be added in the method if array [med] == array [p1] == array [p2], Find by sequentially comparing magnitude between the two pointers, to find the minimum.

answer

import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        int p1=0;
        int p2=array.length-1;
        int med=0;
        if(array.length==0){
            return 0;
        }
        while(p2-p1!=1){
            med=(p1+p2)/2;
            if(array[med]>=array[p1]){
                p1=med;
            }
            else
                p2=med;
        }
        if(array[p1]<=array[p2]){
            return array[p1];
        }
        else
            return array[p2];
    }
}

 

Topic 9

We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). 
n- <= 39 

Thinking

 Fibonacci sequence is a typical scene recursive function, but since there are many repeated calculation nodes, thus recursive solution has a serious efficiency.

answer

public class Solution {
    public int Fibonacci(int n) {
        if(n==0){
            return 0;
        }
        else if(n==1){
            return 1;
        }
        else{
            return Fibonacci(n-1)+Fibonacci(n-2);
        }
    }
}

Topic 9-1

A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

Thinking

This is a Fibonacci number of scenarios cut columns.

1. If there is only one level, then obviously there is only one jump method.

2. If there are two steps, the 2 = 2/1 + 1 = 2, there are two jumps.

3. When n> 2, there are two options, the first time when a jump just behind the remaining n-1 to stage n-1 corresponds to the step jump; 2 when the first skip, the back rest n-2 corresponding to the jump steps. Thus f (n) = f (n-1) + f (n-2).

4. We have just mentioned above Fibonacci row solution will have too much double-counting, resulting in low efficiency. That this series of intermediate terms we get by saving to avoid double counting, or pushed from the bottom up, i.e. have f according to f (1) and f (0) (2), (2) and obtained according to f (1), and f f (3) (where f (1) is in a last calculated, and f (2) has been obtained by previous calculation, add together the two), we are able to process these in the previous item to save. Can simplify the calculation, the time complexity is O (n) of the solution.

 answer 

public class Solution {
    public int JumpFloor(int target) {
        int[] result={1,2};
        if(target<=2){
            return result[target-1];
        }
        int floorOne=1;
        int floorTwo=2;
        int floorN=0;
        for(int i=3;i<=target;++i){
            floorN=floorOne+FloorTwo; 
            floorOne = FloorTwo; 
            FloorTwo = floorN; 
        } 
        Return floorN; 
    } 
}

 

Topic 9-2 

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

Thinking

 This question may be the solution in two ideas:

1. derived by mathematical calculation, we can find that f (n) = 1 + 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ··· + 2 ^ (n-2), then summing the geometric series can be calculated out f (n) = 2 ^ (n-1)

2. Analysis can be f (n) = f (1) + f (2) + ··· + value f (n-1) +1, an iterative process can also be preserved by the previous question on the layer, calculated value.

answer

public class Solution {
    public int JumpFloorII(int target) { int sum=1; int first=1; while(target>1){ sum+=first; first=sum; target--; } return sum; } }

 

Topic 9-3

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?

Thinking

By drawing, we can see that this is an application Feibolaqi number of columns. f (n) = f (n-1) + f (n-2)

Note boundary value 0

answer

public class Solution {
    public int RectCover(int target) {
        int[] result={0,1,2};
        if(target<=2){
            return result[target];
        }
        int firstN=1;
        int twoN=2;
        int sumN=0;
        for(int i=3;i<=target;i++){
            sumN=firstN+twoN;
            firstN=twoN;
            twoN=sumN;
        }
        return sumN;
    }
}

10 title

An integer that indicates the number of the output of the binary number 1 in. Wherein a negative number indicates a complement.

Thinking

1.Integer.bitCount (int n); // This function can be used to calculate the number of integers in binary representation is 1.

2. integer divided by 2 on the right and mathematically equivalent, but the efficiency of the division ratio of a shift operation is much lower, so instead of using the shift operation as multiplication and division in the programming process.

3. We can continue to perform integer input of the right, and then determine whether or not the last one. Continue right until the whole integer becomes zero so far.

4. So how this idea is determined whether or not the far right is an integer 1, with the integer 1 may be employed with the operation. Since 1 In addition to the right-most bit is 1, the other bits are 0, 1 and then if the integer calculation result is 1, indicating that the rightmost one is the integer 1, and 0 otherwise.

5. However, this method if you enter a negative number, we need to ensure that the shift is negative, then the highest bit shifted to 1. If the operation has been done right, this number will fall into an infinite loop. To avoid falling into an infinite loop, you can not enter the right number n, and n and by 1 and do arithmetic, determine the lowest bit of n is 1. And the number of cycles of this line of thinking is equivalent integer binary digits.

6. simplify this operation, then we will consider the case of a number minus 1.

n:  100100

n-1:  100011

n&(n-1):100000

Each of n and n-1 & calculation performed, a binary 1 is removed from right to left rightmost.

answer 

public class Solution {
    public int NumberOf1one(int n) {
        int count=0;
        while(n!=0){ if(n&1)
          count++;
        n=n>>1; } return count;
    }


    public int NumberOf1two(int n) {
        int count=0;
     unsigned int flag=1;
        while(flag!=0){
            if(n&flag)
          count++;
        flag=flag<<1;
        }
        return count;
    } 
  
  public
int NumberOf1three(int n) { int count=0; while(n!=0){ ++count; n=(n-1)&n; } return count; } }

 

 

Guess you like

Origin www.cnblogs.com/lyeeer/p/12215817.html