Created
March 13, 2013 03:50
-
-
Save charlespunk/5149254 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given an M*N matrix in which each row and each column is sorted in ascending order, write a method to find an element. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean findElement(int element, int[] matrix){ | |
int row = 0; | |
int column = matrix[0].length - 1; | |
while(row < matrix.length && column >= 0){ | |
if(element == matrix[row][column]) return true; | |
else if(element < matrix[row][column]) column--; | |
else row++; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment