Search a 2D Matrix II【原创】

Search a 2D Matrix II:

python学了还么两周可能有些地方写的很新手大家不要笑我

解题思路:

首先当数组为1维数组时if in 直接查找即可

当为 m x n 维数组时:

matrix为行增列增的有序矩阵 所以对行和列采用折半查找,即可判断出值所在的新的矩阵范围,一步步缩小,若存在则某一次的行折半或者列折半肯定会遍历到,否则不存在于矩阵中

每一行的第一个元素为最小值,每一列最后一个元素为最大值 若此行的第一个元素比target要大,那肯定在行号小于此行的行中,若此列的最后一个元素比target还要小,那肯定在列号大于此列的列中,重新划定缩小矩阵的范围再次进行查询即可,比如查找15

‘‘‘
Search a 2D Matrix II
‘‘‘
class Solution:
    # @param {integer[][]} matrix
    # @param {integer} target
    # @return {boolean}
    # @author sallency 32448732
    # @date 2015/07/27
    def searchMatrix(self, matrix, target):
        #matrix rows and cols
        rows = len(matrix)
        #如果是二维数组
        if (type(matrix[0]) == list) :
            cols = len(matrix[0])
            if (isInMatrix(matrix, rows - 1, 0, target)) :
                return True
            else :
                return False
        #如果是一位数组直接用遍历
        else :
            if target in matrix :
                return True
            else :
                return False

‘‘‘
isInMatrix用来判断target是否在matrix中,因为matrix是行增列增的有序矩阵
用折半查找先找出包含target的行边界,再在新的范围内排除列边界
逐渐缩小范围,若target存在matrix中,在某次新的边界判定划分时会被遍历到
否则最终范围为0未找到
@param matrix 查找的matrix主体
@param row_border col_border 当前范围查询的新边界
@target 查找的target
‘‘‘
def isInMatrix(matrix, row_border, col_border, target):
        rows_list = []
        #from 0 to row_border
        for i in range(row_border + 1):
            #列标为cols_border
            rows_list.append(matrix[i][col_border])
        rows_l, rows_r = 0, row_border
        #binary search to find the row gt target first
        while (rows_l <= rows_r) :
            rows_avg = (rows_l + rows_r) / 2
            if (rows_list[rows_avg] == target) :
                return True
            elif (rows_list[rows_avg] > target) :
                #row_avg-1 is the value border of target
                #overflow
                if (rows_avg - 1 < 0) :
                    return False
                #new range row border
                if (rows_list[rows_avg - 1] < target) :
                    row_border = rows_avg - 1
                    break
                else :
                    rows_r = rows_avg - 1
            else :
                rows_l = rows_avg + 1
        if (rows_l > rows_r) :
            row_border = rows_l - 1
        #the new matrix row range is from 0 to row_border
        #binary search to find the col gt target first
        cols_list = []
        for j in range(col_border, len(matrix[0])):
            cols_list.append(matrix[row_border][j])
        cols_l, cols_r = col_border, len(matrix[0]) - 1
        while (cols_l <= cols_r) :
            cols_avg = (cols_l + cols_r) / 2
            if (matrix[row_border][cols_avg] == target) :
                return True
            elif (matrix[row_border][cols_avg] > target) :
            	#overflow
                if (cols_avg - 1 < 0) :
                    return False
                 #new range col border
                if (matrix[row_border][cols_avg - 1] < target) :
                    col_border = cols_avg
                    break
                else :
                    cols_r = cols_avg - 1
            else :
                cols_l = cols_avg + 1
        if (cols_l > cols_r) :
            col_border = cols_l
        #the new matrix col range is from col_border to max_left
        #if overflow
        if (row_border > len(matrix) - 1 or col_border > len(matrix[0]) - 1) :
            return False
        else :
            return isInMatrix(matrix, row_border, col_border, target)
时间: 2024-10-13 23:17:09

Search a 2D Matrix II【原创】的相关文章

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description 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 ascend

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II 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 ascendin

LeetCode -- Search a 2D Matrix &amp; Search a 2D Matrix II

Question: Search a 2D Matrix 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 from left to right. The first integer of each row is greater than the la

[LeetCode][JavaScript]Search a 2D Matrix II

Search a 2D Matrix II 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 ascendin

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

[leedcode 240] Search a 2D Matrix II

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.

[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

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.

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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.

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public