Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created March 13, 2013 03:50
Show Gist options
  • Save charlespunk/5149254 to your computer and use it in GitHub Desktop.
Save charlespunk/5149254 to your computer and use it in GitHub Desktop.
Given an M*N matrix in which each row and each column is sorted in ascending order, write a method to find an element.
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