LeetCode-74&240.Search a 2D Matrix
来源:互联网 发布:足彩单式出票软件 编辑:程序博客网 时间:2024/06/11 02:14
74 https://leetcode.com/problems/search-a-2d-matrix/
Write an efficient algorithm that searches for a value in an m x n 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.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
Given target = 3
, return true
.
显然用二分查找
public bool SearchMatrix(int[,] matrix, int target) { int m = (int)matrix.GetLongLength(0); int n = (int)matrix.GetLongLength(1); if (matrix[0, 0] > target || matrix[m - 1, n - 1] < target) return false; int start = 0, end = m - 1, mid,row; while (start<=end) { mid = (start + end) / 2; if (matrix[mid, 0] == target) return true; if (matrix[mid, 0] < target) start = mid + 1; else end = mid - 1; } row = end; start = 0; end = n - 1; while (start<=end) { mid = (start + end) / 2; if (matrix[row, mid] == target) return true; if (matrix[row, mid] < target) start = mid + 1; else end = mid - 1; } return false; }
240 https://leetcode.com/problems/search-a-2d-matrix-ii/
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]]
Given target = 5
, return true
.
Given target = 20
, return false
.
比较容易想到的办法及就是分别找行和列的max值
然后逐一判断
public static bool SearchMatrix(int[,] matrix, int target) { int m = (int)matrix.GetLongLength(0); int n = (int)matrix.GetLongLength(1); if (matrix[0, 0] > target || matrix[m - 1, n - 1] < target) return false; int l = 0, r = n - 1, mid=0,maxi,maxj; while (l<=r) { mid = (l + r) / 2; if (matrix[0, mid] == target) return true; if (matrix[0, mid] > target) r = mid - 1; else l = mid + 1; } maxj = r; l = 0; r = m - 1; while (l <= r) { mid = (l + r) / 2; if (matrix[mid,0] == target) return true; if (matrix[mid,0] > target) r = mid - 1; else l = mid + 1; } maxi = r; for (int i = 0; i <= maxi; i++) { for (int j = 0; j <= maxj; j++) { if (matrix[i, j] == target) return true; } } return false; }
第二种方法十分技巧,从左下角开始遍历, 如果大于当前就往右走, 小于就往上走。(也可以从右上角开始),参考https://segmentfault.com/a/1190000004288673
public bool SearchMatrix(int[,] matrix, int target) { int m = (int)matrix.GetLongLength(0); int n = (int)matrix.GetLongLength(1); if (matrix[0, 0] > target || matrix[m - 1, n - 1] < target) return false; int i = m - 1, j = 0; while (i >= 0 && j < n) { if (matrix[i, j] == target) return true; if (matrix[i, j] > target) i--; else j++; } return false; }
0 0
- Leetcode 74 Search a 2D Matrix
- LeetCode 74: Search A 2D Matrix
- LeetCode 74 Search a 2D Matrix
- [leetcode 74] Search a 2D Matrix
- [LeetCode 74]Search a 2D Matrix
- leetcode || 74、Search a 2D Matrix
- leetcode-74 Search a 2D Matrix
- Leetcode[74]-Search a 2D Matrix
- 【Leetcode】Search a 2D Matrix #74
- LeetCode(74) Search a 2D Matrix
- Leetcode #74 Search a 2D Matrix
- Leetcode #74 Search a 2D Matrix
- leetcode 74: Search a 2D Matrix
- Leetcode#74||Search a 2D Matrix
- leetcode 74 : Search a 2D Matrix
- [Leetcode]#74 Search a 2D Matrix
- 【leetcode】【74】Search a 2D Matrix
- leetcode 74:Search a 2D Matrix
- win2012R2配置双网卡绑定
- adb logcat 查看日志命令行用法(简版)
- ZOJ,PKU--训练题分类
- nyoj1248 海岛争霸(第七届河南省程序设计大赛)
- 使用adb shell 进入手机修改文件的权限
- LeetCode-74&240.Search a 2D Matrix
- POJ 3635 Dragon Balls (并查集)
- #学习笔记#(63)拖拽HTML5-drag-drop座位表
- 设计模式的六大原则
- 3.2用栈判断回文字符串
- Matlab与C/C++混合编程接口应用总结
- 成长
- Handler的消息机制原理
- 写点什么