Offer 1 wins the title: ordered two-dimensional array search

Topic 1: ordered two-dimensional array search

Title Description

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Thinking

A thought: violence algorithm

Each element of the array, traversing comparison.

Time complexity: O (N * N);

Not used to: the conditions of an orderly array

 /**
     *
     * @param target  待查询的目标
     * @param array  二维数组
     * @return
     * 思路:遍历整个二维数组,穷举所有的目标进行比较
     * 210ms 16784k
     */
    public boolean Find1(int target, int [][] array) {

        for(int i=0;i<array.length;i++){//遍历行
            for(int j=0;j<array[0].length;j++){//遍历列
                if(array[i][j]==target){
                    return true;
                }
            }
        }
        return false;
    }

Thinking two: using a binary search for each line

Because each row of data is ordered, is a one-dimensional ordered array, you can perform binary search this line, time complexity is O (log2n)

The row of n sequential use binary search, the time complexity is: O (nlogn)

//193ms
public static  boolean Find4(int target,int[][] array){

        for(int i=0;i<array.length;i++){//遍历每一行

            int left = 0;
            int right = array[0].length-1;
            int mid = (left+right)/2;

            while(left<=right){
                mid = (left+right)/2;
                if(array[i][mid]>target){//数组中的值比目标值大,则从左半部分再查找
                    right = mid-1;
                }else if(array[i][mid]<target){
                    left=mid+1;
                }else{
                    return true;//找到
                }

            }

        }
        return  false;

    }

Three ideas: comparison from the top right or bottom left hand conversion

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-yJmv0j8i-1579150525148) (images / 01.png)]

Matrix is ​​ordered,

From the bottom left corner of view, up diminishing numbers, the right numbers increase,

Therefore, from the lower left corner to start looking when you want to find digital numbers larger than the lower-left corner. Right;

When looking for a figure than the lower left corner of the digital hours, move up

Compared in turn from the top right corner of the lower left corner

//212ms 
public static boolean Find3(int target, int [][] array){
        int i=array.length-1; //定义最后一行下标
        int j =0;//左下角
         while(i>=0 && j<array[0].length) {//

             if (array[i][j] > target) {//左下角数字比待比较 数字 大上移
                 i--;
             } else if (array[i][j] < target) {//左下角数字比当前数字小,右移
                 j++;
             } else {
                 return true;
             }

         }

         return false;
    }

Violence algorithm: O (N * N) 210ms

Inline binary search: O (NLOGN) 193ms

Bottom right - top left: 212ms

Published 101 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/ZHOUJIAN_TANK/article/details/104002824