题意:每行每列递增,判断数组是否有target,数组有*重复*元素。
思路1:二分,从最右上角开始比较,<target,该行排除,>target,该列排除。

    public class Solution {
    public boolean Find(int target, int [][] array) {
        if (array == null || array.length == 0) {
            return false;
        }
        if (array[0] == null || array[0].length == 0) {
            return false;
        }
        int rows = array.length;
        int cols = array[0].length;
        boolean found = false;
        int row = 0;
        int col = array[0].length - 1;
        while(row < rows && col >= 0){
            if (array[row][col] == target){
                found = true;
                break;
            } else if (array[row][col] < target){
                row++;
            } else {
                col--;
            }
        }
        return found;
    }
    }
测试用例:
1、数组有要找的元素(该元素是最大,最小、介于之间)
2、没有要找的元素(该元素是超出最大,最小、介于之间没有)
3、空指针

posted on 2017-06-30 17:59  Scarlett meng  阅读(195)  评论(0编辑  收藏  举报