码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Search a 2D Matrix

时间:2014-09-08 00:58:06      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   div   sp   log   on   

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int low=0, high=matrix.length-1;
        while (low<high) {
            int mid = (low+high)/2+1;
            if (matrix[mid][0] > target) high = mid-1;
            else low = mid;
        }
        
        int index1 = low;
        
        low = 0;
        high = matrix[0].length-1;
        while (low<high) {
            int mid = (low+high)/2+1;
            if (matrix[index1][mid] > target) high = mid-1;
            else low = mid;
        }
        
        return matrix[index1][low] == target;
    }
}

 

[LeetCode] Search a 2D Matrix

标签:style   blog   color   io   ar   div   sp   log   on   

原文地址:http://www.cnblogs.com/yuhaos/p/3960972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!