The next day

Keywords: Java

Today, I did four questions. They are wonderful. The last two questions are a little difficult.

1. Matrix Covering

Title Description: We can use 21 small rectangles to cover larger rectangles horizontally or vertically. How many ways are there to cover a 2n rectangle with n 21 small rectangles without overlapping?
Solution: When the length is 21, there is only one way to pave. When the length is 2*2, two pieces can be laid horizontally and two pieces can be laid vertically. So considering this recursive method, we can either add only one brick at the end of the first n-1 method or lay two bricks vertically in the first n-2 method, so the recursive formula is: F[n]=F[n_1]+F[n_2]F[n]=F[n-1]+F[n-2]F[n]=F[n] = F [n_1]+F[n_2] F[n]=F[n] = F [n_1]+F[n_2], which is essentially the number of Ferbonard thefts.

public class Solution{
	int[] F=new int[1001];
	public void init(){
		F[1]=1;
		F[2]=2;
		for(int i=3;i<=1000;i++)
		{
			F[i]=F[i-1]+F[i-2];
		}

	}

	public int RectCover(int target){
		this.init();
		return F[target];
	}
}

2. Two-dimensional array lookup

Title Description: In a two-dimensional array (each one has the same length), each row is sorted in the order of increasing from left to right, and each column is sorted in the order of increasing from top to bottom. Please complete a function, input such a two-dimensional array and an integer, to determine whether the array contains the integer.
Solution ideas: The previous solution ideas can also be, but the time complexity is too high, you can start to search from the top right corner of the graph, if the target is larger than the point where the array is located, the number of rows plus one, otherwise the number of columns minus one, you can return after finding, otherwise you can not find it.

public class Solution {
    public boolean Find(int target, int [][] array) {
        int row = array.length;
        int col = array[0].length;
        boolean found=false;
        int i=0,j=col-1;
        while(i<row&&j>=0)
        {
            if(array[i][j]==target)
            {
                found=true;
                break;
            }
            else if(target>array[i][j])
                i++;
            else
                j--;
        }
        return found;
       }
}

3. Print the linked list from end to end

Title Description: Enter a linked list and return an ArrayList in the order from end to end.
Traverse the list and press the values into the stack, then go out one by one, and return to the list.

import java.util.Arraylist;
import java.util.Stack;
public class Solution{
	public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
	Stack<Integer> s = new Stack<Integer>();
	ArrayList<Integer> array= new ArrayList<Integer>();
	while(listNode!=null)
	{
		s.push(listNode.val);
		listNode=listNode.next;
	}
	while(!s.empty()){
		array.add(s.pop());
	}
	return array;
	}
}

4. Arrange arrays into the smallest number

Title Description: Input a positive integer array, splice all the digits in the array into one number, print the smallest of all the digits that can be spliced. For example, the input array {3, 32, 321} prints out the minimum number of the three numbers that can be arranged as 32123.
Thoughts on problem solving: I feel that the main purpose of this test is to examine some API, Array's sort function, and the use of Comparator class, which is not too difficult. If a b > b a, then a > b, otherwise a < B and then make rules for size, then sort with sort function.

import java.util.Arrays;
import java.util.Comparator;
public class Solution{
	public String PrintMinNumber(int[] numbers){
		String[] sortedNumber=new String[numbers.length];
		for(int i=0;i<numbers.length;i++)
		{
			sortedNumber=String.valueOf(numbers[i]);
		}
		Arrays.sort(numbers, new Comparator()
		{
			@Override
			public int compare(String a, String b){
				return (a+b).compareTo(b+a);
			}
		});
		String ans=new String();
		for(int i=0;i<sortedNumber.length;i++)
		{
			ans=ans+sortedNumber[i];
		}
		return ans;
		
	}

}

Posted by dunnsearch on Thu, 19 Sep 2019 05:29:07 -0700