Search in 2D array of [sword finger Offer]

Sword finger Offer question

Search in 2D array -- sword finger Offer 1

Title Description

In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in ascending order from left to right,
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.

thinking

1. Search from the top right corner according to the characteristics of the array
 2. If it is equal, return true
 3. If the target value is greater than the current value, ignore the current line and continue searching
 4. If the target value is less than the current value, ignore this column and continue searching

Code

package com.codinginterviews;

/**
 * Title:
 * Search in 2D array -- sword finger Offer 1
 * 
 * Title Description:
 * In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in ascending order from left to right,
 * 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.
 * 
 */
public class TwoDimensionalArrayLookup
{
    /**
     * Train of thought:
     * 1,According to the characteristics of the array, search from the top right corner
     * 2,If equal, return true
     * 3,If the target value is greater than the current value, ignore the line and continue to search
     * 4,If the target value is less than the current value, ignore this column and continue searching
     */
    public boolean find(int target, int [][] array) {
        if (array == null) {
            return false;
        }
        
        int rowNum = array.length;
        if (rowNum <= 0) {
            return false;
        }
        
        int colNum = array[0].length;
        if (colNum <= 0) {
            return false;
        }
        
        // Navigate to the top right element
        int i = 0;
        int j = rowNum - 1;
        // Current element
        int curVal;
        // ergodic
        while (i < rowNum && j >= 0) {
            curVal = array[i][j];
            if (curVal == target) {
                return true;
            // Ignore this line and continue searching
            } else if (curVal < target) {
                i++;
            // Ignore this column to continue searching
            } else {
                j--;
            }
            
        }
        return false;
    }
    
    public static void main(String[] args)
    {
        int[][] array = {{1,2,7},{3,5,8},{5,6,9}};
        TwoDimensionalArrayLookup instance = new TwoDimensionalArrayLookup();
        System.out.println(instance.find(1, array));
        System.out.println(instance.find(5, array));
        System.out.println(instance.find(9, array));
        System.out.println(instance.find(4, array));
        System.out.println(instance.find(10, array));
    }

}

Tags: less

Posted on Sun, 10 Nov 2019 11:24:34 -0500 by kevski