Selection class sorting -- simple selection, heap sort

1. In a two-dimensional array, 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, and determine whether the array contains the integer.

Ideas:
1. The matrix is ​​ordered, from the lower left corner, the numbers decrease upwards and increase to the right;
2. Therefore, start the search from the lower left corner, and when the number to be found is larger than the number in the lower left corner, move to the right;
3. To find a number smaller than the lower left number, move up.

public class Solution
{
    public static void main(String[] args)
    {
        int array[][] = {{1,2,3},{4,5,6},{7,8,9}};
        if(Find(6,array))
            System.out.println("数据含有这样的数");
        else
            System.out.println("数据不含有这样的数");
    }

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324827172&siteId=291194637