74. Search a 2D Matrix
来源:互联网 发布:高考必知文学常识 编辑:程序博客网 时间:2024/06/14 04:10
Search a 2D Matrix
代码
使用二分法
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int col_num = matrix[0].length; int row_num = matrix.length; int begin = 0,end = row_num * col_num - 1; while(begin <= end){ int mid = (begin + end) / 2; int mid_value = matrix[mid/col_num][mid%col_num];//这步很巧 if( mid_value == target){ return true; }else if(mid_value < target){ //Should move a bit further, otherwise dead loop. begin = mid+1; }else{ end = mid-1; } } 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
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- Leetcode 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix LeetCode
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix
- 74. Search a 2D Matrix
- leetcode 74. Search a 2D Matrix
- 时间转换
- getOutputStream() has already been called for this response异常的原因和解决方法[转]
- 深入了解Object类
- springamqp的exchange
- Swift-协议
- 74. Search a 2D Matrix
- c++第五次作业
- 自动生成场景中的XML表
- 移动网页设计9大原则——第2部分
- 【流媒體】Android 实时视频采集—Cameara预览采集
- vickate_将一个正整数分解质因数
- Qt程序提示: 启动程序失败,路径或者权限错误解决方案
- Android Studio 创建aidl文件,用于进程间通讯
- 日期处理2