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