Two-dimensional array to find the target value

Source: Internet
Author: User

The

has a two-dimensional array, each row, each column is incremented, finds a target value, and returns true if it exists otherwise false. The
starts at the lower-right corner, or to the left if it is greater than the target value, or downward if it is less than the target.

package searchtest;
        public class Erweiarray {private static Boolean find (int[][] A, int val) {Boolean find = false;
        int rows = A.length;
        int columns = A[0].length;
            if (A! = null && rows > 0 && columns > 0) {int row = 0; int column = Columns-1;
                    The subscript for the row and column is 0 while (Row < rows && column >= 0) {if (a[row][column] = = val) {
                    find = true;
                Break
                } else if (A[row][column] > val) {column--;
                } else {row++;
    }}} return to find; 
                public static void Main (string[] args) {int[][] tes = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13},
        {6, 8, 11, 15}};
        System.out.print (Find (TES, 18));

    System.out.print (Find (TES, 7)); }

}

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.