LeetCode240:Search a 2D Matrix II
来源:互联网 发布:事业单位网站域名 编辑:程序博客网 时间:2024/06/03 20:39
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
.
Subscribe to see which companies asked this question
//时间复杂度为O(m+n)class Solution{public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int m = matrix.size();int n = matrix[0].size();int i = 0, j = n - 1;while (i < m && j >= 0){if (matrix[i][j] == target)return true;else if (matrix[i][j] > target)j--;elsei++;}return false;}};
0 0
- LeetCode240:Search a 2D Matrix II
- leetcode240---Search a 2D Matrix II
- LeetCode240:Search a 2D Matrix II
- Leetcode240: Search a 2D Matrix II
- Leetcode240. Search a 2D Matrix II
- leetcode240. Search a 2D Matrix II
- leetcode240. Search a 2D Matrix II
- LeetCode240——Search a 2D Matrix II
- LeetCode240. Search a 2D Matrix II题解
- LeetCode240. Search a 2D Matrix II题解
- LeetCode240. Search a 2D Matrix II二分查找+分治法
- Search a 2D Matrix II
- [刷题]Search a 2D Matrix II
- lintcode:Search a 2D Matrix II
- #leetcode#Search a 2D Matrix II
- Search a 2D Matrix II
- [LeetCode] Search a 2D Matrix II
- leetcode: Search a 2D Matrix II
- 使用Cornerstone更新后工程打不开问题的解决办法
- Android触摸事件的分发、拦截、处理
- C++外部程序修改exe文件属性信息的版本信息
- java正则表达式获取指定HTML标签的指定属性值
- memcached客户端和连接池
- LeetCode240:Search a 2D Matrix II
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
- 缓存
- 模板方法模式(Template Method Pattern)
- ARM Linux设备树
- Autosqli——A better sqlmap
- Android Studio上修改SVN地址
- 【知识整理】本人整理ie6兼容问题的解决方法
- curl 作为 http 客户端使用示例