题目:

编写一个高效的算法来判断 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

解题思路:

首先应该找到target所在行,通过一行的首尾元素与target的大小关系,确定其所在行数

if((matrix[i][0] <= target) && (matrix[i][n - 1] >= target))

在确定好行数后,就在一行内采用二分查找算法寻找target

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(null == matrix || 0 == matrix.length || 0 == matrix[0].length)
return false;
int m = matrix.length;
int n = matrix[0].length;
int i;
for(i = 0; i < m; i++)
{
if((matrix[i][0] <= target) && (matrix[i][n - 1] >= target))
break;
}
if(i == m)
return false;
if(matrix[i][0] == target || matrix[i][n - 1] == target)
return true;
int left = 0;
int right = n -1;
int mid = (left + right) / 2;
while(left <= right)
{
if(target == matrix[i][mid])
return true;
else if(target < matrix[i][mid])
{
right = mid - 1;
mid = (left + right) / 2;
}
else
{
left= mid + 1;
mid = (left + right) / 2;
}
}
return false;
}
}

leetcode 74 搜索二维矩阵 java的更多相关文章

  1. Java实现 LeetCode 74 搜索二维矩阵

    74. 搜索二维矩阵 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: ...

  2. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  3. LeetCode 74——搜索二维矩阵

    1. 题目 2. 解答 若矩阵为空,比如 [], [[]],此时直接返回 false. 若目标值小于矩阵第一个元素或者大于矩阵最后一个元素,则目标值不在矩阵范围内,直接返回 false. 其他情况下, ...

  4. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: ma ...

  5. Leetcode 74. 搜索二维矩阵 C+

    二分法,先对行二分找出结果可能存在的行,再对这一行二分查找.O(Log m+Log n),m.n分别为矩阵的高和宽. class Solution { public: bool searchMatri ...

  6. LeetCode:搜索二维矩阵【74】

    LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...

  7. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  8. 【leetcode】74. 搜索二维矩阵

    题目链接:传送门 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  9. Java实现 LeetCode 240 搜索二维矩阵 II(二)

    240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...

随机推荐

  1. mock 的独立使用

    public class Air21QueryMileStoneJobTest{ @InjectMocks Air21QueryMileStoneJob air21QueryMileStoneJob ...

  2. Spring IO Platform介绍

    为什么要用Spring IO Platform 今天无意间看到了一个关键词:"Spring IO Platform",第一直觉是不是有关于IO方面的框架或者包呢,查了一下,居然是为 ...

  3. apache启动不了, 查找错误

    今天apache启动不了, 本来以为是端口冲突, 用 cmd-> netstat -aon|findstr "80"  或 tasklist|findstr "80 ...

  4. How To Manually Install Oracle Java on Ubuntu

    Introduction Java is a programming technology originally developed by Sun Microsystems and later acq ...

  5. Changing the load order/delay the start of the Server service

    THE INFORMATION IN THIS ARTICLE APPLIES TO: Secure FTP Server (All Versions) EFT Server (All Version ...

  6. Lua加密

    两种方式:一种用luac,一种用luajit luac加密: 1.lua本身可以使用luac将脚本编译为字节码(bytecode)从而实现加密,去官网下载Lua源代码包(http://www.lua. ...

  7. dsm winscp 获得 root 权限修改上传文件

    使用DSM开了ssh只可以用admin登陆scp没有权限上传文件.可以用以下方法. ssh 登陆 dsm sudo -i  取得root权限 修改 /etc/sudoers 文件中 %administ ...

  8. java.lang.VerifyError: Inconsistent stackmap frames at branch target 81

    java项目中有如下代码: @RequestMapping(value = "/getMxList") @ResponseBody public Map<String, Ob ...

  9. Grapher

    [Grapher] You use Grapher to visualize and analyze implicit and explicit equations. You can graph eq ...

  10. 9.Palindrome Number (INT)

    Determine whether an integer is a palindrome. Do this without extra space. class Solution { public: ...