Array of sword finger offer

Search in binary array

1. In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in ascending order from left to right, and each column is sorted in ascending order 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.
1. Violence law:
Time complexity n^2

public class Solution {
    public boolean Find(int target, int [][] array) {
        for(int i = 0;i<array.length;i++){
            for(int j=0;j<array[0].length;j++){
                if(target == array[i][j]){
                    return true;
                }
            }
        }
        return false;
    }
}

Because the matrix is ordered
Idea 1: treat each row as an array of orderly increasing,
Using binary search,
By traversing each row to get the answer,
Time complexity is nlogn

public class Solution {
    public boolean Find(int target, int[][] array) {
        for (int i=0;i<array.length;i++) {
            int low = 0;//Get the minimum subscript in each row
            int high = array[i].length-1;//Get the maximum subscript in each row
            while(low <= high){
                int mid = (low+high) / 2;
                if (target>array[i][mid]){
                    low = mid + 1 ;
                }else if (target<array[i][mid]){
                    high = mid - 1 ;
                }else{
                    return true;
                }
            }
        }
        return false;
    }
}

Idea 2: make use of the law that two-dimensional array increases from top to bottom and from left to right,
Then select the element a[row][col] in the upper right or lower left corner to compare with target,
When target is smaller than element a[row][col], then target must be to the left of the row in element a,
col -.
When target is greater than element a[row][col], then target must be under the column of element a,
row + +; time complexity O(n)

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

Duplicate numbers in array

2. All numbers in an array of length n are in the range of 0 to n-1. Some numbers in the array are repeated, but I don't know how many are. I don't know how many times each number is repeated. Please find any duplicate number in the array. For example, if the input length is 7 array {2,3,1,0,2,5,3}, the corresponding output is the first repeated number 2.

Idea 1: sort the input array, and then judge whether there is the same number in the adjacent position. If there is, assign a value to the duplication to return, otherwise continue the comparison. Time complexity: O(nlogn)O(nlogn)
Space complexity: O(1)O(1)

import java.util.Arrays;
public class Solution {
    
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    Special attention should be paid here ~ return any repeated one, assign duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if (numbers==null || length==0){
            return false;
        }
        Arrays.sort(numbers);
        for(int i=0;i<length-1;i++){
            if(numbers[i]==numbers[i+1]){
                duplication[0] = numbers[i];
                return true;
            }
        }

        return false;
    }
}

Idea 2: use HashSet to solve the problem. Scan the array from the beginning to the end. Scan to a number each time. Judge whether the current number exists in the HashSet. If it exists, it will be repeated. Assign a value to duplication and return it. Otherwise, add the number to the HashSet
Time complexity: O(n)O(n)
Spatial complexity: O(n)O(n)

import java.util.HashSet;
import java.util.Set;
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    Special attention should be paid here ~ return any repeated one, assign duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if (numbers==null || length==0){
            return false;
        }
        Set<Integer> set = new HashSet<>();
        for (int i=0;i<length;i++){
            if (set.contains(numbers[i])){
                duplication[0] = numbers[i];
                return true;
            }
            set.add(numbers[i]);
        }
        return false;
    }
}

Train of thought 3:
The length of the array is n and all the numbers are in the range of 0 to n-1. We can "return" the numbers we encounter each time. When a number finds that its "position" is occupied by the same number, it will repeat.
Scan the whole array. When a number with index i is scanned, first compare whether the number (m) is equal to i, if so, then scan the next number; if not, then compare m with the number M. If M is equal to the number of the m, it means there is a repetition; if M is not equal to the number of the m, then exchange M with the number of the m, put M "back", and repeat the process of comparison and exchange until the number of repetition is found

Here's a chestnut:
Take the array {2,3,1,0,2,5,3} as an example
When i = 0, nums[i] = 2! = I, judge that nums[i] is not equal to nums[nums[i]], exchange nums[i] and nums[nums[i]], and the array after exchange is: {1,3,2,0,2,5,3}
At this time, i = 0, nums[i] = 1! = I, judge that nums[i] is not equal to nums[nums[i], exchange nums[i] and nums[nums[i], and the array after exchange is: {3,1,2,0,2,5,3}
At this time, i = 0, nums[i] = 3! = I, judge that nums[i] is not equal to nums[nums[i]], exchange nums[i] and nums[nums[i]], and the array after exchange is: {0,1,2,3,2,5,3}
Now i = 0, nums[i] = 0 = i, continue to the next group
When i = 1, nums[i] = 1 = i, continue to the next group
When i = 2, nums[i] = 2 = i, continue to the next group
When i = 3, nums[i] = 3 = i, continue to the next group
When i = 4, nums[i] = 2! = I, judge that nums[i] is equal to nums[nums[i]], duplicate occurs, and the assignment returns
Time complexity: O(n)O(n)
Space complexity: O(1)O(1)

Building a product array

Title Description
Given an array A[0,1 , n-1], build an array B[0,1 , n-1], where element B[i]=A[0]A[1] *A[i-1]A[i+1]… *A[n-1]. Division cannot be used. (Note: Provisions B[0] = A[1] * A[2] * * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)
Idea 1: time complexity of brute force cracking On^2

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {
        int[] B = new int[A.length];
        for (int i=0;i<B.length;i++){
            B[i]=1;
            for(int j =0;j<A.length;j++){
                if (j!=i){
                    B[i]=A[j]*B[i];
                }
            }
            
        }
        return B;
    }
}

Train of thought 2:
B[i]=A[0]A[1] A[i-1]A[i+1]… A[n-1]. Consider A[0]A[1] A [I-1] and a [i + 1] The product of two parts of A[n-2]A[n-1].
That is to say, B[i] is divided into two parts by A[i] term. The effect is equivalent to a diagonal matrix.

The first for loop is used to calculate the number of ranges in Figure 1, and the second for loop is used to calculate the number of ranges in Figure 2.
Time complexity On

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {

        int[] B = new int[A.length];
        B[0] = 1 ;
        for (int i=1;i<B.length;i++){
            B[i] = A[i-1] * B[i-1];
        }
        int temp = 1;
        for (int j=B.length-1;j>=0;j--){
            B[j] = temp *  B[j];
            temp = A[j] * temp;
        }
        return B;
    }
}
Published 12 original articles, won praise 0, visitors 296
Private letter follow

Tags: Java

Posted on Mon, 16 Mar 2020 07:27:14 -0400 by prakash911