[Sword refers to Offer learning] [interview question 3: Search in two-dimensional array], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview question 3: Search in two-dimensional array], sword refers to offer

Question: 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. Complete a function, input a two-dimensional array and an integer to determine whether the array contains the integer.

Public class Test03 {/*** 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. * Complete a function, input a two-dimensional array and an integer to determine whether the array contains the integer. * <P/> * Rule: first, select the number in the upper-right corner of the array. If the number is equal to the number to be searched, the search process ends: * If the number is greater than the number to be searched, remove the column where the number is located. If the number is smaller than the number to be searched, remove the row where the number is located. * That is to say, if the number to be searched is not in the upper-right corner of the array, the row or column is excluded from the array's search range every time, in this way, each step can narrow down the * search range until the number to be searched is found or the search range is null. ** @ Param matrix the array to be searched * @ param number the number to be searched * @ return the search result. true indicates the result, false not found */public static boolean find (int [] [] matrix, int number) {// input condition judgment if (matrix = null | matrix. length <1 | matrix [0]. length <1) {return false;} int rows = matrix. length; // number of rows in the array int cols = matrix [1]. length; // Number of columns in the array rows int row = 0; // The starting row number int col = cols-1; // column number starting from the start // make sure the position to be searched is within the array while (row> = 0 & row <rows & col> = 0 & col <cols) {if (matrix [row] [col] = number) {// exit return true if it is found;} else if (matrix [row] [col]> number) {// if the number is greater than the number to be searched, it indicates that the number to be searched is on the left of the current number col --; // The number of columns is reduced by one, indicates moving to the left} else {// if the number found is smaller than the number to be searched, it indicates that the number to be searched is under the current number row ++; // The number of rows plus one, indicates moving down} return false;} public static void main (String [] args) {int [] [] matrix = {1, 2, 8, 9 }, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}; System. out. println (find (matrix, 7); // The number of queries in the array System. out. println (find (matrix, 5); // the number to be searched is not in the array System. out. println (find (matrix, 1); // the number to be searched is the smallest number in the array System. out. println (find (matrix, 15); // the number to be searched is the largest number in the array System. out. println (find (matrix, 0); // the number to be searched is smaller than the smallest number in the array. System. out. println (find (matrix, 16); // the number to be searched is greater than the maximum number in the array. System. out. println (find (null, 16); // robustness test, input null pointer }}


Running result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.