[LinkedIn] Find target number in 2D sorted array (matrix)
2015-04-03 14:33
369 查看
Say I’m given a 2d array where all the numbers in the array are in increasing order from left to right and top to bottom.
What is the best way to search and determine if a target number is in the array?
What is the best way to search and determine if a target number is in the array?
//Starting from the top-right, if target is smaller then go left, if it is larger then go down public boolean search2DMatrix(int[][] matrix, int target) { int row = 0, col = matrix[0].length-1; while (row < matrix.length && col >= 0) { if (matrix[row][col] == target) return true; else if (matrix[row][col] < target) row++; else col--; } return false; }
相关文章推荐
- find path/route in a maze 2 d matrix
- lintcode---Find Elements in Matrix
- Find minimum number in Rotated sorted array.
- Find minimum number of people to reach to spread a message across all people in twitter
- [LinkedIn] Word Distance Finder (Find distance in dictionary)
- lintcode-medium-Kth Smallest Number in Sorted Matrix
- LintCode "Kth Smallest Number in Sorted Matrix"
- Find Elements in Matrix-LintCode
- longest increasing continuous subsequence in a 2D matrix
- find k closest point to origin point in 2d plane
- find two numbers such that they add up to a specific target number
- Find n‘th number in a number system with only 3 and 4
- Find the total area covered by two rectilinear rectangles in a 2D plane. 208MM
- How to find number of a specific integer in an array of Fixnums ruby
- 【Leetcode】maximum Rangtangle in 2D matrix
- Find Elements in Matrix
- DP27 2D矩阵中最大的矩形和 Maximum sum rectangle in a 2D matrix @geeksforgeeks
- Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This
- Array(11) -- Largest Rectangle in Histogram, Maximal Rectangle, Find the Duplicate Number
- Find n‘th number in a number system with only 3 and 4