Sword finger offer practice

Keywords: Java

/**Decadent for many days, come back to repent*/

1, Search of two dimensional array

Title: in a two-dimensional array (the length of each one-dimensional array is the same), 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

When learning c, i and j of the two-dimensional array are always fixed in advance. Two dimensional team leader,

In many languages, arr.length is used to find the array length. For example, the coordinates of the elements in the first row of arr.length in java are (0,0), (0,1), (0,2), (0,3);

And the two-dimensional array is composed of another one-dimensional array. Then, arr[i].length can find the column length of row I. In this way, the number of rows and columns is determined, and the two-dimensional array is traversed.

Secondly, we notice that the number of two-dimensional arrays is increasing. When searching, we can search violently or by dichotomy. Because of increasing, the search interval can be reduced according to the target value and the size of the current element.

public class Main {
    public static void main(String []args) {
        int[][] arr={
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,18}};
                System.out.println(Find(3,arr)?"yes":"no");
                System.out.println(Find(15,arr)?"yes":"no");
      
    }
    public static  boolean Find(int target, int[][] matrix){
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return false;
        int rows = matrix.length,cols=matrix[0].length;
        int r= 0,c=cols-1;//From the top right corner
        while (r<=rows-1 && c>=0){
                if(target == matrix[r][c])
                    return true;
                else if (target > matrix[r][c])
                    r++;
                else
                    c--;
            }
            return false;
    }
}

2, Duplicate numbers in array

In an array of length N, all numbers 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: we can create another array, which is initialized to 0, and then traverse the current target array target. If target[i] is 1, then add the value of temp[1] to 1. After traversing, traverse the temp array and find the first subscript whose value is greater than 2, that is, the first repeated number in target;

Compared with other people's ideas, i am very low. Others: adjust the element whose value is i to the i position;

The number in the array is in the range of 0 to n-1. If there are no duplicate numbers in this array, the data in the corresponding I position is also I. This array can be rearranged to scan every number in the array. When a number with index I is scanned, first compare whether the number (m) is equal to I. If so, scan for the next number. If not, compare it with the m-th number. If it is equal, duplicate data will be found. Otherwise, exchange the i-th number with the m-th number. Repeat the process of comparison and exchange until the repeated numbers are found.

 

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> res = new ArrayList<>();
        int len = nums.length;
        if (len == 0) {
            return res;
        }
        for (int i = 0; i < len; i++) {
            while (nums[nums[i] - 1] != nums[i]) {
                swap(nums, i, nums[i] - 1);
            }
        }
        for (int i = 0; i < len; i++) {
            if (nums[i] - 1 != i) {
                res.add(nums[i]);
            }
        }
        return res;
    }

    private void swap(int[] nums, int index1, int index2) {
        if (index1 == index2) {
            return;
        }
        nums[index1] = nums[index1] ^ nums[index2];
        nums[index2] = nums[index1] ^ nums[index2];
        nums[index1] = nums[index1] ^ nums[index2];
    }
}

/Source: Buckle( LeetCode)/
 

3. Build product array

Given 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 [n-1]. Division cannot be used

First of all, I will think of violence multiplication, n^2 time complexity

For example, A[]={1,2,3,4,5}

The results are A[0]=0,A[1]=1*2,A[2]=1*2*3,A[3]=0,A[0]=0,

This is simple. Go straight to code 2

public int[] test(int[] A){
    int n = A.length;
    int[]B = new int[n]; 
    for(int i=0,p=1;i<n;p*=A[i],i++) /*From left to right*/
        B[i]=p;
    for(int i=0,p=1;i<n;p*=A[i],i--) /*From left to right*/
        B[i]*=p;    
    return B;
}

 

102 original articles published, 35 praised, 7979 visited
Private letter follow

Posted by webproclaim on Sun, 08 Mar 2020 22:45:58 -0700