zoukankan      html  css  js  c++  java
  • LeetCode 面试题04. 二维数组中的查找

    我的LeetCode:https://leetcode-cn.com/u/ituring/

    我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Algorithmcii

    LeetCode 面试题04. 二维数组中的查找

    题目

    在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    示例:

    现有矩阵 matrix 如下:
    
    [
      [1,   4,  7, 11, 15],
      [2,   5,  8, 12, 19],
      [3,   6,  9, 16, 22],
      [10, 13, 14, 17, 24],
      [18, 21, 23, 26, 30]
    ]
    给定 target = 5,返回 true。
    
    给定 target = 20,返回 false。
    

    限制:

    • 0 <= n <= 1000

    • 0 <= m <= 1000

    注意:本题与主站 240 题相同:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解题思路

    思路1-二分查找

    因为所有的数都是排好序的,那么我们需要找到中间值,然后采用二分查找;
    分析后可以发现右上角的值就是中间位置值,所以可以从此处开始查找;
    n为横,m为竖,查找的逻辑:

    • 以右上角到左下角为轴,轴点连接的横竖的数据可拉直看为一个有序区间;
    • 横坐标代表较小值,竖坐标代表较大值,从右上角[row,col]开始搜索,初始row=0,col=m-1;
      • 若当前值小于目标值,则row++;
      • 若当前值大于目标值,则col--;

    算法复杂度:

    • 时间复杂度: $ {color{Magenta}{Omicronleft(logn ight)}} $
    • 空间复杂度: $ {color{Magenta}{Omicronleft(1 ight)}} $

    算法源码示例

    package leetcode;
    
    /**
     * @author ZhouJie
     * @date 2020年4月28日 下午4:45:26 
     * @Description: 面试题04. 二维数组中的查找
     *
     */
    public class LeetCode_Offer_04 {
    	/**
    	 * @author: ZhouJie
    	 * @date: 2020年4月28日 下午5:51:04 
    	 * @param: @param matrix
    	 * @param: @param target
    	 * @param: @return
    	 * @return: boolean
    	 * @Description: 1-从右上角向左下角把相连的横竖行拉直就是一个连续的递增数组;
    	 *
    	 */
    	public boolean findNumberIn2DArray(int[][] matrix, int target) {
    		// 特例判断
    		if (matrix == null || matrix.length == 0) {
    			return false;
    		}
    		int m = matrix.length;
    		int n = matrix[0].length;
    		int row = 0, col = n - 1;
    		// 二分查找,以右上角的位置作为中间位置,小于target就row++,否则就col--
    		while (row < m && col > -1) {
    			if (matrix[row][col] > target) {
    				col--;
    			} else if (matrix[row][col] < target) {
    				row++;
    			} else {
    				return true;
    			}
    		}
    		// 越界仍为找到时
    		return false;
    	}
    }
    
    
    
  • 相关阅读:
    What is the purpose of FormsAuthenticationTicket isPersistent property?
    Forms Authentication configurations
    What is the difference between localStorage, sessionStorage, session and cookies?
    Set-Cookie
    申威机器信息
    systemctl 关闭图形界面的办法
    sourcetree 使用
    在 Web 项目中应用 Apache Shiro
    移动web开发框架
    从0开始搭建symphony
  • 原文地址:https://www.cnblogs.com/izhoujie/p/12819203.html
Copyright © 2011-2022 走看看