【GO语言学习】LeetCode74. 搜索二维矩阵---二分法
2019-05-28 18:04
1781 查看
题目
编写一个高效的算法来判断 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
代码
[code]func searchMatrix(matrix [][]int, target int) bool { if len(matrix)==0{ return false } if len(matrix[0])==0{ return false } m:=len(matrix) n:=len(matrix[0]) i := 0 j := m*n-1 mid := (i+j)/2 for i<=j{ mid = (i+j)/2 if matrix[mid/n][mid%n]==target{ return true }else if matrix[mid/n][mid%n] > target{ j = mid - 1 }else{ i = mid + 1 } } return false }
结果
相关文章推荐
- leetcode74. 搜索二维矩阵
- 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与240题:搜索二维矩阵
- leetcode的python实现 刷题笔记74:搜索二维矩阵(二分查找的思想)
- LeetCode 240 搜索二维矩阵
- 用python写leetcode【9】 --搜索二维矩阵 II(240)
- LeetCode240. 搜索二维矩阵 II(python)
- leetcode 74. Search a 2D Matrix-矩阵搜索|二分查找
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
- Leetcode240.搜索二维矩阵
- LeetCode [74] 搜索二维矩阵
- 【LeetCode-面试算法经典-Java实现】【074-Search a 2D Matrix(搜索二维矩阵)】
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
- LeetCode 74. Search a 2D Matrix(搜索矩阵)
- LeetCode 74. Search a 2D Matrix(二维矩阵查找)
- Leetcode刷题26-240.搜索二维矩阵 II(C++)