167 - 74 搜索二维矩阵 - 001
题目
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 输出: true
示例 2:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 输出: false
解答
这个数组的条件如此严苛,令我有点想把它拍扁成一位数组,然后二分查找。。
或者直接调原生api,一个个数组找😂
这题不配中等难度哈哈哈
原生api
Runtime: 68 ms, faster than 85.18% of Python3 online submissions for Search a 2D Matrix.
Memory Usage: 14.9 MB, less than 5.88% of Python3 online submissions for Search a 2D Matrix.
Runtime: 56 ms, faster than 66.91% of JavaScript online submissions for Search a 2D Matrix.
Memory Usage: 34.3 MB, less than 100.00% of JavaScript online submissions for Search a 2D Matrix.
Runtime: 64 ms, faster than 93.52% of Python3 online submissions for Search a 2D Matrix.
Memory Usage: 14.8 MB, less than 5.88% of Python3 online submissions for Search a 2D Matrix.
Runtime: 60 ms, faster than 41.85% of JavaScript online submissions for Search a 2D Matrix.
Memory Usage: 36.6 MB, less than 50.00% of JavaScript online submissions for Search a 2D Matrix.
双指针
就是把二维数组当成一位数组,用公式算出对应一位数组的位置。
Runtime: 64 ms, faster than 93.52% of Python3 online submissions for Search a 2D Matrix.
Memory Usage: 14.8 MB, less than 5.88% of Python3 online submissions for Search a 2D Matrix.
Runtime: 56 ms, faster than 66.91% of JavaScript online submissions for Search a 2D Matrix.
Memory Usage: 34.3 MB, less than 100.00% of JavaScript online submissions for Search a 2D Matrix.
Last updated
Was this helpful?