leetcode: Search a 2D Matrix
来源:互联网 发布:网络电视盒什么牌子好 编辑:程序博客网 时间:2024/05/23 21:58
由于每一行都有序,后一行起始大于前一行最后的元素。 所以我们遍历每一行的末尾和target比较,当大于target时在这一行寻找target即可。 要时刻注意终止循环的条件。
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length; if( m==0 ) { return false; } int n = matrix[0].length; int j=n-1; int i=0; while(i<m) { if( matrix[i][j]==target ) { return true; } else if( matrix[i][j]<target ) { if( j!=n-1) { return false; } else { i++; } } else { j--; if( j<0 ) { return false; } } } return false; }}
0 0
- [leetcode][Search] Search a 2D Matrix
- LeetCode: Search a 2D Matrix
- LeetCode Search a 2D Matrix
- [Leetcode] Search a 2D Matrix
- LeetCode : Search a 2D Matrix
- [LeetCode] Search a 2D Matrix
- Leetcode 74 Search a 2D Matrix
- leetcode 90: Search a 2D Matrix
- [LeetCode]Search a 2D Matrix
- [Leetcode] Search a 2-D matrix
- [Leetcode]Search a 2D Matrix
- [leetcode]Search a 2D Matrix
- LeetCode-Search a 2D Matrix
- [leetcode] Search a 2D Matrix
- LeetCode - Search a 2D Matrix
- LeetCode:Search a 2D Matrix
- LeetCode 74: Search A 2D Matrix
- leetcode之Search a 2D Matrix
- LITTELFUSE(力特)样片申请与经验
- cocos2d-x 是如何渲染的
- 阿里大数据比赛排名获取2
- 微信公众平台开发—利用OAuth2.0获取微信用户基本信息
- 淘宝在线订购应用实践
- leetcode: Search a 2D Matrix
- MinGW初步使用
- ios delegate 用法
- javascript: typeof
- 再看数据库——(3)触发器
- Android之Inflate()方法用途
- cocos2dx 3.1从零学习(三)——Touch事件(回调,反向传值)
- Lucene创建索引入门案例
- Cocos2d-x源码解析(1)——地图模块(3)