LeetCode刷题笔录Search a 2D Matrix

UE5 GAS 学习笔记 1.2游戏标签 这一节对GameplayTag游戏标签进行详细的讲解。 阅读详情

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 last integer of the previous row.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

想法是先在第一列进行Binary Search,找到元素可能所在的那一行。再在这一行进行BS,找到这个元素。找不到就是false。

复杂度是O(logm+logn)

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        int[] firstColumn = new int[m];
        for(int i = 0; i < m; i++){
            firstColumn[i] = matrix[i][0];
        }
        int column = columnSearch(firstColumn, 0, m - 1, target);
        if(column == -1)
            return false;
            
        int[] targetRow = matrix[column];
        int row = rowSearch(targetRow, 0, n - 1, target);
        if(row == -1)
            return false;
        
        return true;
        
    }
    //binary search to find the row that might contain the target
    int columnSearch(int[] array, int low, int high, int target){
        if(low > high)
            return -1;
        int mid = (low + high) / 2;
        if(array[high] < target)
            return high;
        if(low == high){
            if(array[mid] <= target)
                return mid;
            else
                return -1;
        }
        else{
            //if the array has more than one element, and array[mid]<=target<array[mid+1]
            //then the target might be in the row matrix[mid]
            if(array[mid] <= target && array[mid + 1] > target)
                return mid;
        }
        
        if(array[mid] < target)
            return columnSearch(array, mid + 1, high, target);
        else
            return columnSearch(array, low, mid - 1, target);
    }
    //binary search on an array to return the specific index of the target
    //return -1 if not found
    int rowSearch(int[] array, int low, int high, int target){
        if(low > high)
            return -1;
        int mid = (high + low) / 2;
        if(array[mid] == target)
            return mid;
        if(array[mid] < target){
            return rowSearch(array, mid + 1, high, target);
        }
        else{
            return rowSearch(array, low, mid - 1, target);
        }
    }
}

另外这个矩阵其实可以看成一个大的排好序的数组,于是只要用一次二分法就行了。

复杂度是O(log(mn))。其实和上面的算法一样。

	public boolean searchMatrix(int[][] matrix, int target) {
		int m = matrix.length;
		int n = matrix[0].length;
		int start = 0;
		int end = m * n - 1;
		
		while(start <= end){
			int mid = (start + end) / 2;
			int midX = mid / n;
			int midY = mid % n;
			
			if(matrix[midX][midY] == target)
				return true;
			else if(matrix[midX][midY] > target){
				end = mid - 1;
				continue;
			}
			else{
				start = mid + 1;
				continue;
			}
				
		}
		return false;		
    }


Unreal 从入门到进阶 之 如何实现Pixel Streaming Unreal Engine 5 (UE5) 的 Pixel Streaming 是一项基于云端的实时流媒体技术,允许将 UE5 应用的高质量图形渲染结果通过 WebRTC 协议传输到用户的浏览器或轻量级设备,同时将用户输入(如鼠标、键盘、触控)回传到服务器处理。UE的插件相对比较好用,但是就是环境下载总是失败。希望这篇文章对你有所帮助。 阅读详情

相关推荐

Smali语言基础语法

定义、声明、调用 Smali数据类型 定义:与Java相同,Smali中也有一套数据类型体系,且该体系实质上是同Java一一对照的。 示例1:Smali——Java数据类型对照表 PS:如上图所示,Smali语言中的数据类型的关键词基本上为Java中同名数据类型的首字母大写,需要着重记忆的例外为:1.long类型的关键词为大写J;2.boolean类型的关键词为大写Z;3.数组的关键词为左半边中括号[;4.object类型(各种类)的关键词为大写L加上该类的全类名路径,路径中的层级用/分割。

aketoshknight的博客 1万+

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

likecool21的专栏 1万+

【SoloPi】SoloPi使用2-功能使用,录制回放

Soloπ是什么 Soloπ是一个无线化、非侵入式的Android自动化工具,公测版拥有录制回放、性能测试、一机多控三项主要功能,能为测试开发人员节省宝贵时间。 录制回放功能 Soloπ拥有录制操作的能力,用户只需要通过Soloπ执行用例步骤,Soloπ就能够将用户的操作记录下来,并且支持在各个设备上进行回放,这一切都能够在手机上独立完成。 ...

q_Catherine的博客 5828

LeetCode刷题笔录 Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the

likecool21的专栏 1万+

VS2022下OSG+OSGEARTH开发环境一键配置指南(Win10 x64+CMake自动化构建)

本文详细介绍了在VS2022环境下配置OSG和OSGEARTH开发环境的完整指南,包括软件下载、CMake自动化构建、编译实战及环境验证。通过一键配置脚本和常见问题解决方案,帮助开发者快速搭建3D开发环境,提升开发效率。

weixin_28828243的博客 405

python气象数据可视化学习笔记8——利用matplotlib和ERA5数据绘制时间-高度气象综合廓线图

利用matplotlib和ERA5数据绘制时间-高度气象综合廓线图

qiuylulu的博客 8063

LeetCode刷题笔录 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th

likecool21的专栏 6928

LPDDR6与LPDDR5 ZQ Calibration技术探讨

LPDDR6与LPDDR5 ZQ Calibration技术探讨

zilan23的博客 1098

Stanford Algorithms学习:Clustering 2

这是紧接上面的一道题,比较有趣 Question 2 In this question your task is again to run the clustering algorithm from lecture, but on a MUCH bigger graph. So big, in fact, that the distances (i.e., edge costs

likecool21的专栏 2567

数仓建模—维度建模之维度表

首先要学会识别维度,维度是相对稳定的、可抽象具有共性的、固有的(天然的)描述度量或事实的上下文属性信息,失去维度信息的度量信息是没有意义的或者说是失去维度信息的事实表是没有意义的。其次要掌握维度表的设计原则和设计流程,从而更加科学的设计维度表,其实这些东西都是术,我们一直都说一句话大道至简,其实如果你掌握了精髓,抛开这些术也可以做到得心应手,这就像张无忌练太极剑一样,忘记了才能达到道的境界,也就是无我,如果做不到就记住这些原则加以应用从而达到术的境界。

2401_84052244的博客 4361

网络工程师软考通关指南:网络新技术与应用深度解析

网络安全与管理是网络工程师必须掌握的核心技能,华为设备在企业网络中广泛应用。建议考生在理解安全原理的基础上,重点掌握华为特有的命令体系和配置方法,通过模拟实验加深理解。下篇文章将深入讲解"网络优化与QoS"章节,涵盖流量调度、拥塞避免、服务质量等关键技术,敬请期待。

Programming Talk 252

Stanford Algorithms学习:Clustering

第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。 Question 1 In this programming problem and the next you'll code up the clustering algorithm from lecture for computing a

likecool21的专栏 2507

[毕设项目-MCP+ENSP]一句话搞定ENSP自动化:MCP + LLM实战(架构图、工具清单、踩坑全解)

用 MCP + LLM 把“人话”变成对华为 ENSP 的安全自动化操作,三款工具覆盖配置/查询/落盘,配套架构、实战与排坑清单。

逐梦苍穹的博客 2537

LeetCode 刷题笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode刷题笔录Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:

likecool21的专栏 1296

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode刷题笔录Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

likecool21的专栏 1109

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

LeetCode刷题笔录 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题比较简单

likecool21的专栏 1043

LeetCode刷题笔录Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

likecool21的专栏 1001

LeetCode刷题笔录 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 随手写的一个,正常的读出每一位的数字然后反序: import java.lang.Math; public class Solution { public int revers

likecool21的专栏 941

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 112

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 203

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 175
上一篇: LeetCode刷题笔录Minimum Path Sum
下一篇: LeetCode刷题笔录Container With Most Water
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值