Sword finger offer questions 6 to 10 (JAVA)

Posted by djr587 on Mon, 12 Aug 2019 08:20:33 +0200

Chapter 6:

Moving the initial elements of an array to the end of the array is called the rotation of the array. Input a rotation of an array with a non-emission reduction order, and output the smallest element of the rotation array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. NOTE: All elements given are greater than 0. If the array size is 0, return 0.

Idea: Set the minimum value to be the first element of the array, traverse the array, and then replace it if it is smaller than that, and interrupt the loop directly.

Code:

import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length == 0){
            return 0;
        }
        int min = array[0];import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length == 0){
            return 0;
        }//Exclusion of 0
        int min = array[0];
        for(int i =1;i<array.length;i++){
            if(array[i] < min){
                min = array[i];//Find the minimum and jump out of the loop
                break;
            }
        }
        return min;
    }
}
        for(int i =1;i<array.length;i++){
            if(array[i] < min){
                min = array[i];
                break;
            }
        }
        return min;
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Chapter 7:

We all know the Fibonacci sequence. Now we need to input an integer n. Please output the nth item of the Fibonacci sequence (starting from 0, the 0th item is 0).

n<=39

Idea: If you use recursion, always supermemory, so use loops to achieve. Fibonatch sequence: 1,1,2,3,5,8,13,21... Wait. Write an array of size 3. The first number and the second number are the front number, and the third number is the sum of the first two numbers.

Code:

public class Solution {
    public int Fibonacci(int n) {

        if(n==0)
        {return 0;}
        if(n==1||n==2)
        {return 1;}
        int num[]={1,1,2};
        for(int i=3;i<n;i++)
        {
            num[0]=num[1];
            num[1]=num[2];
            num[2]=num[0]+num[1];//Simple implementation with array
        }
        return num[2];
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Chapter 8:

A frog can jump up a step or two at a time. Find out how many jumping methods the frog jumps on an n-step (different results in different order).

Train of thought: suppose that there are f(n) jumping methods in order n, and f(n-1) jumping methods in order n-1. Suppose the first jump is followed by f(n-1) jumping method, and the first jump is followed by f(n-2) jumping method.

So the total jump method is f(n)=f(n-1)+f(n-2).

Code:

public class Solution {
    public int JumpFloor(int target) {
        if(target<=0){return -1;}
        if(target==1){return 1;}
        if(target==2){return 2;}//Exclude special circumstances
        return JumpFloor(target-1)+JumpFloor(target-2);//recursion
        
    }
}

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Question 9:

A frog can jump up a step or two at a time... It can also jump to level n. Find out how many jumping methods the frog can use to jump up an n-step.

Ideas:

Similar to the seventh question, suppose that there are f(n-1) jumps after the first jump.

Then the total jump method f(n)=f(n-1)+f(n-2)+f(n-3)....+f(1), and f(n-1)=f(n-2)+f(n-3)....+f(1)

So f(n)=2*f(n-1)

Code:

public class Solution {
    public int JumpFloorII(int target) {
        if(target==0||target==1)
        {return target;}//Exclude special circumstances
        return 2*JumpFloorII(target-1);//recursion
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Topic 10:

(Reference: Programmer Chogory)

We can use a small rectangle of 2*1 to cover larger rectangles horizontally or vertically. How many ways are there to cover a large 2*n rectangle with n small 2*1 rectangles without overlapping?

Ideas:

Code:

public class Solution {//Finding the Law
    public int RectCover(int target) {
        if(target==1){return 1;}
        if(target==2){return 2;}
        int a=1,b=2,sum=0;
        for(int i=3;i<=target;++i)
        {
            sum=a+b;
            a=b;
            b=sum;
        }
        return sum;
    }
}

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Topics: Java