Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.

This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • Integers in each column are sorted from up to bottom.
  • No duplicate integers in each row or column.
Example

Consider the following matrix:

[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]

Given target = 3, return 2.

分析:

因为数组里的数有上面三个特性,所以我们可以从左下角开始找。如果当前值比target大,明显往上走,比target小,往右走,如果一样斜上走。Time complexity: (O(m + n)) (m = matrix.length, n = matrix[0].length)

 public class Solution {
public int searchMatrix(int[][] matrix, int target) {
// check corner case
if (matrix == null || matrix.length == 0 || matrix[].length == 0) {
return ;
}
// from bottom left to top right
int x = matrix.length - ;
int y = ;
int count = ; while (x >= && y < matrix[].length) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
count++;
x--;
y++;
}
}
return count; }
}

Search a 2D Matrix I

Write an efficient algorithm that searches for a value in an mn matrix.

This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.
Example

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

分析:

根据数组的特点,我们需要找出一个大范围(row),然后再确定小范围。

 public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == || matrix[].length == ) return false;
int rowCount = matrix.length, colCount = matrix[].length;
if (matrix[][] > target || matrix[rowCount - ][colCount - ] < target) return false; int row = getRowNumber(matrix, target);
int column = getColumnNumber(matrix, row, target); return matrix[row][column] == target; } public int getRowNumber(int[][] matrix, int target) {
int start = , end = matrix.length - , width = matrix[].length;
while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[mid][width - ] == target) {
return mid;
} else if (matrix[mid][width - ] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
} public int getColumnNumber(int[][] matrix, int row, int target) {
int start = , end = matrix[].length; while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return mid;
} else if (matrix[row][mid] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
}
}

最新文章

  1. wpf ListBox删除
  2. 修改Firebug字体
  3. windows 10 上office2016 word崩溃的解决方案
  4. Twig模版语言入门
  5. matlab(数组、矩阵)
  6. MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】
  7. EF 的 霸气配置
  8. POJ2104 区间第k小
  9. POJ 3362 Protecting the Flowers
  10. 一个C#操作RabbitMQ的完整例子
  11. MIPI协议-DSI
  12. .NET WebAPI中使用Session使用
  13. 动态在线扩容root根分区大小的方法详解
  14. pthread_join与pthread_detach细节问题
  15. iOS 开发-Certificate、App ID和Provisioning Profile之间的关系
  16. MySQL事物系列:2:事物的实现
  17. SQL Server 表分区之水平表分区
  18. 非常不错的一个JS分页效果代码
  19. iOS 第三方框架-MJRefresh
  20. mysql的字符拼接

热门文章

  1. Oracle创建表格报ORA-00906:缺失左括号错误解决办法
  2. XMLHttpRequest的POST中文表单问题解决方案
  3. sersync + rsync 实现文件的实时同步
  4. Java基础-String、StringBuffer、StringBuilder
  5. [Asp.net mvc]实体更新异常:存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。
  6. 【CodeForces 611C】New Year and Domino
  7. 【蒟蒻の进阶PLAN】 置顶+持续连载
  8. 找回Reshaprer的Alt+Enter快捷键的方法
  9. POJ 2752 Seek the Name, Seek the Fame
  10. 单机redis多端口实例+keepalived高可用