Find in a two-dimensional array-New Ket Net-Sword Point offer

Source: Internet
Author: User

1. Description of the problem

In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.

2. Problem analysis

Horizontal, vertical double loop lookup

3. Source code
 PackageWww.nowcoder.com.conquerOffer.array;/*** Find in a two-dimensional array * In a two-dimensional array, each row is sorted from left to right in ascending order, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer. * http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking *@authorSunny **/ Public classTwodimensionalarrayfind {/*** Two-dimensional array lookup *@paramArray Two-dimensional arrays *@paramTarget Destination Number *@returnwhether found (true: found; false: Not found)*/     Public BooleanFind (int[] Array,inttarget) {        //determine if a two-dimensional array is empty        if(NULL= = Array | | Array.Length <= 0)            return false;  for(inti = 0; i < Array.Length; i++){            //determines whether a one-dimensional array is empty            int[] arr =Array[i]; if(NULL= = Arr | | Arr.length <= 0)                Continue;  for(intj = 0; J < Arr.length; J + +){                if(target = =Arr[j]) {System.out.println ("array[" + i + "[" + j + "]=" +target); return true; }            }        }        return false; }         Public Static voidMain (string[] args) {int[] Array =New int[][]{{1, 2},{3, 4, 5, 6},{},{7, 8, 9, 10, 11},{12}};//int[][] array = new int[][]{};Twodimensionalarrayfind Twodimensionalarrayfind =NewTwodimensionalarrayfind (); BooleanIsfind = Twodimensionalarrayfind.find (Array, 10);    System.out.println (Isfind); }}

4. Operation effect
1 array[3][3]=102true
View Code

Find in a two-dimensional array-New Ket-Sword Point offer

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.