Leetcode面试高频题分类刷题总结

https://zhuanlan.zhihu.com/p/349940945

以下8个门类是面试中最常考的算法与数据结构知识点。

排序类(Sort):

  • 基础知识:快速排序(Quick Sort), 归并排序(Merge Sort)的原理与代码实现。需要能讲明白代码中每一行的目的。快速排序时间复杂度平均状态下O(NlogN),空间复杂度O(1),归并排序最坏情况下时间复杂度O(NlogN),空间复杂度O(N)
  • 入门题目:
    • Leetcode 148. Sort List
    • Leetcode 56. Merge Intervals
    • Leetcode 27. Remove elements
  • 进阶题目:
    • Leetcode 179. Largest Number
    • Leetcode 75. Sort Colors
    • Leetcode 215. Kth Largest Element (可以用堆的解法替代)
    • Leetcode 4. Median of Two Sorted Arrays

注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考

链表类(Linked List):

  • 基础知识:链表如何实现,如何遍历链表。链表可以保证头部尾部插入删除操作都是O(1),查找任意元素位置O(N)
  • 基础题目:
    • Leetcode 206. Reverse Linked List
    • Leetcode 876. Middle of the Linked List

注意:快慢指针和链表反转几乎是所有链表类问题的基础,尤其是反转链表,代码很短,建议直接背熟。

  • 进阶题目:
    • Leetcode 160. Intersection of Two Linked Lists
    • Leetcode 141. Linked List Cycle (Linked List Cycle II)
    • Leetcode 92. Reverse Linked List II
    • Leetcode 328. Odd Even Linked List

堆(Heap or Priority Queue)、栈(Stack)、队列(Queue)、哈希表类(Hashmap、Hashset):

  • 基础知识:各个数据结构的基本原理,增删查改复杂度。
  • Queue题目:
    • Leetcode 225. Implement Stack using Queues
    • Leetcode 346. Moving Average from Data Stream
    • Leetcode 281. Zigzag Iterator
    • Leetcode 1429. First Unique Number
    • Leetcode 54. Spiral Matrix
    • Leetcode 362. Design Hit Counter
  • Stack题目:
    • Leetcode 155. Min Stack (follow up Leetcode 716 Max Stack)
    • Leetcode 232. Implement Queue using Stacks
    • Leetcode 150. Evaluate Reverse Polish Notation
    • Leetcode 224. Basic Calculator II (I, II, III, IV)
    • Leetcode 20. Valid Parentheses
    • Leetcode 1472. Design Browser History
    • Leetcode 1209. Remove All Adjacent Duplicates in String II
    • Leetcode 1249. Minimum Remove to Make Valid Parentheses
    • Leetcode 735. Asteroid Collision
  • Hashmap/ Hashset题目:
    • Leetcode 1. Two Sum
    • Leetcode 146. LRU Cache (Python中可以使用OrderedDict来代替)
    • Leetcode 128. Longest Consecutive Sequence
    • Leetcode 73. Set Matrix Zeroes
    • Leetcode 380. Insert Delete GetRandom O(1)
    • Leetcode 49. Group Anagrams
    • Leetcode 350. Intersection of Two Arrays II
    • Leetcode 299. Bulls and Cows
    • Leetcode 348 Design Tic-Tac-Toe
  • Heap/Priority Queue题目:
    • Leetcode 973. K Closest Points
    • Leetcode 347. Top k Largest Elements
    • Leetcode 23. Merge K Sorted Lists
    • Leetcode 264. Ugly Number II
    • Leetcode 1086. High Five
    • Leetcode 88. Merge Sorted Arrays
    • Leetcode 692. Top K Frequent Words
    • Leetcode 378. Kth Smallest Element in a Sorted Matrix
    • Leetcode 295. Find Median from Data Stream (标准解法是双heap,但是SortedDict会非常容易)
    • Leetcode 767. Reorganize String
    • Leetcode 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit (这个题用单调双端队列、TreeMap、双heap都可以)
    • Leetcode 895. Maximum Frequency Stack

二分法(Binary Search):

  • 基础知识:二分法是用来解法基本模板,时间复杂度logN;常见的二分法题目可以分为两大类,显式与隐式,即是否能从字面上一眼看出二分法的特点:要查找的数据是否可以分为两部分,前半部分为X,后半部分为O
  • 显式二分法:
    • Leetcode 34. Find First and Last Position of Element in Sorted Array
    • Leetcode 33. Search in Rotated Sorted Array
    • Leetcode 1095. Find in Mountain Array
    • Leetcode 162. Find Peak Element
    • Leetcode 278. First Bad Version
    • Leetcode 74. Search a 2D Matrix
    • Leetcode 240. Search a 2D Matrix II
  • 隐式二分法:
    • Leetcode 69. Sqrt(x)
    • Leetcode 540. Single Element in a Sorted Array
    • Leetcode 644. Maximum Average Subarray II
    • Leetcode 528. Random Pick with Weight
    • Leetcode 1300. Sum of Mutated Array Closest to Target
    • Leetcode 1060. Missing Element in Sorted Array
    • Leetcode 1062. Longest Repeating Substring
    • Leetcode 1891. Cutting Ribbons
    • Leetcode 410. Split Array Largest Sum (与1891类似)

双指针(2 Pointer):

  • 基础知识:常见双指针算法分为三类,同向(即两个指针都相同一个方向移动),背向(两个指针从相同或者相邻的位置出发,背向移动直到其中一根指针到达边界为止),相向(两个指针从两边出发一起向中间移动直到两个指针相遇)
  • 背向双指针:(基本上全是回文串的题)
    • Leetcode 409. Longest Palindrome
    • Leetcode 125. Valid Palindrome (I、II)
    • Leetcode 5. Longest Palindromic Substring
    • Leetcode 647. Palindromic Substrings
  • 相向双指针:(以two sum为基础的一系列题)
    • Leetcode 1. Two Sum (这里使用的是先排序的双指针算法,不同于hashmap做法)
    • Leetcode 167. Two Sum II - Input array is sorted
    • Leetcode 15. 3Sum
    • Leetcode 16. 3Sum Closest
    • Leetcode 18. 4Sum
    • Leetcode 454. 4Sum II
    • Leetcode 277. Find the Celebrity
    • Leetcode 11. Container With Most Water
    • Leetcode 186 Reverse Words in a String II
  • 同向双指针:(个人觉得最难的一类题,可以参考下这里 TimothyL:Leetcode 同向双指针/滑动窗口类代码模板)
    • Leetcode 283. Move Zeroes
    • Leetcode 26. Remove Duplicate Numbers in Array
    • Leetcode 395. Longest Substring with At Least K Repeating Characters
    • Leetcode 340. Longest Substring with At Most K Distinct Characters
    • Leetcode 424. Longest Repeating Character Replacement
    • Leetcode 76. Minimum Window Substring
    • Leetcode 3. Longest Substring Without Repeating Characters
    • Leetcode 1004 Max Consecutive Ones III
    • Leetcode 1658 Minimum Operations to Reduce X to Zero

宽度优先搜索(BFS):面试中最常考的

  • 基础知识:
    • 常见的BFS用来解决什么问题?(1) 简单图(有向无向皆可)的最短路径长度,注意是长度而不是具体的路径(2)拓扑排序 (3) 遍历一个图(或者树)
  • BFS基本模板(需要记录层数或者不需要记录层数)
  • 多数情况下时间复杂度空间复杂度都是O(N+M),N为节点个数,M为边的个数
  • 基于树的BFS:不需要专门一个set来记录访问过的节点
    • Leetcode 102 Binary Tree Level Order Traversal
    • Leetcode 103 Binary Tree Zigzag Level Order Traversal
    • Leetcode 297 Serialize and Deserialize Binary Tree (很好的BFS和双指针结合的题)
    • Leetcode 314 Binary Tree Vertical Order Traversal
  • 基于图的BFS:(一般需要一个set来记录访问过的节点)
    • Leetcode 200. Number of Islands
    • Leetcode 133. Clone Graph
    • Leetcode 127. Word Ladder
    • Leetcode 490. The Maze
    • Leetcode 323. Connected Component in Undirected Graph
    • Leetcode 130. Surrounded Regions
    • Leetcode 752. Open the Lock
    • Leetcode 815. Bus Routes
    • Leetcode 1091. Shortest Path in Binary Matrix
    • Leetcode 542. 01 Matrix
    • Leetcode 1293. Shortest Path in a Grid with Obstacles Elimination
    • Leetcode 417. Pacific Atlantic Water Flow
  • 拓扑排序:(https://zh.wikipedia.org/wiki/%E6%8B%93%E6%92%B2%E6%8E%92%E5%BA%8F)
    • Leetcode 207 Course Schedule (I, II)
    • Leetcode 444 Sequence Reconstruction
    • Leetcode 269 Alien Dictionary
    • Leetcode 310 Minimum Height Trees
    • Leetcode 366 Find Leaves of Binary Tree

深度优先搜索(DFS):面试中最常考的(分类的稍微有点粗糙了,没有细分出回溯/分治来,准备找个时间给每个DFS的题标记下是哪种DFS)

  • 基础知识:
    • 常见的DFS用来解决什么问题?(1) 图中(有向无向皆可)的符合某种特征(比如最长)的路径以及长度(2)排列组合(3) 遍历一个图(或者树)(4)找出图或者树中符合题目要求的全部方案
    • DFS基本模板(需要记录路径,不需要返回值 and 不需要记录路径,但需要记录某些特征的返回值)
    • 除了遍历之外多数情况下时间复杂度是指数级别,一般是O(方案数×找到每个方案的时间复杂度)
    • 递归题目都可以用非递归迭代的方法写,但一般实现起来非常麻烦
  • 基于树的DFS:需要记住递归写前序中序后序遍历二叉树的模板
    • Leetcode 543 Diameter of Binary Tree (分治)
    • Leetcode 124 Binary Tree Maximum Path Sum (分治)
    • Leetcode 226 Invert Binary Tree (分治)
    • Leetcode 101 Symmetric Tree (回溯 or 分治)
    • Leetcode 951 Flip Equivalent Binary Trees (分治)
    • Leetcode 236 Lowest Common Ancestor of a Binary Tree (相似题:235、1650) (回溯 or 分治)
    • Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal (分治)
    • Leetcode 104 Maximum Depth of Binary Tree (回溯 or 分治)
    • Leetcode 987 Vertical Order Traversal of a Binary Tree
    • Leetcode 1485 Clone Binary Tree With Random Pointer
    • Leetcode 572 Subtree of Another Tree (分治)
    • Leetcode 863 All Nodes Distance K in Binary Tree
    • Leetcode 1110 Delete Nodes And Return Forest (分治)
  • 二叉搜索树(BST):BST特征:中序遍历为单调递增的二叉树,换句话说,根节点的值比左子树任意节点值都大,比右子树任意节点值都小,增删查改均为O(h)复杂度,h为树的高度;注意不是所有的BST题目都需要递归,有的题目只需要while循环即可
    • Leetcode 230 Kth Smallest element in a BST
    • Leetcode 98 Validate Binary Search Tree
    • Leetcode 270 Cloest Binary Search Tree Value
    • Leetcode 235 Lowest Common Ancestor of a Binary Search Tree
    • Leetcode 669 Trim a Binary Search Tree (分治)
    • Leetcode 700 Search in a Binary Search Tree
    • Leetcode 108 Convert Sorted Array to Binary Search Tree (分治)
    • Leetcode 333 Largest BST Subtree (与98类似) (分治)
    • Leetcode 285 Inorder Successor in BST (I, II)
  • 基于图的DFS: 和BFS一样一般需要一个set来记录访问过的节点,避免重复访问造成死循环; Word XXX 系列面试中非常常见,例如word break,word ladder,word pattern,word search。
    • Leetcode 341 Flatten Nested List Iterator (339 364)
    • Leetcode 394 Decode String
    • Leetcode 51 N-Queens (I II基本相同)
    • Leetcode 291 Word Pattern II (I为简单的Hashmap题)
    • Leetcode 126 Word Ladder II (I为BFS题目)
    • Leetcode 93 Restore IP Addresses
    • Leetcode 22 Generate Parentheses
    • Leetcode 856 Score of Parentheses
    • Leetcode 301 Remove Invalid Parentheses
    • Leetcode 37 Sodoku Solver
    • Leetcode 212 Word Search II (I, II)
    • Leetcode 1087 Brace Expansion
    • Leetcode 399 Evaluate Division
    • Leetcode 1274 Number of Ships in a Rectangle
    • Leetcode 1376 Time Needed to Inform All Employees
    • Leetcode 694 Number of Distinct Islands
    • Leetcode 131 Palindrome Partitioning
  • 基于排列组合的DFS: 其实与图类DFS方法一致,但是排列组合的特征更明显
    • Leetcode 17 Letter Combinations of a Phone Number
    • Leetcode 39 Combination Sum(I, II, III相似, IV为动态规划题目)
    • Leetcode 78 Subsets (I, II 重点在于如何去重)
    • Leetcode 46 Permutation (I, II 重点在于如何去重)
    • Leetcode 77 Combinations (I, II 重点在于如何去重)
    • Leetcode 698 Partition to K Equal Sum Subsets
    • Leetcode 526 Beautiful Arrangement (similar to 46)
  • 记忆化搜索(DFS + Memoization Search):算是用递归的方式实现动态规划,递归每次返回时同时记录下已访问过的节点特征,避免重复访问同一个节点,可以有效的把指数级别的DFS时间复杂度降为多项式级别; 注意这一类的DFS必须在最后有返回值(分治法),不可以用回溯法; for循环的dp题目都可以用记忆化搜索的方式写,但是不是所有的记忆化搜索题目都可以用for循环的dp方式写。
    • Leetcode 139 Word Break II
    • Leetcode 72 Edit Distance
    • Leetcode 377 Combination Sum IV
    • Leetcode 1235 Maximum Profit in Job Scheduling
    • Leetcode 1335 Minimum Difficulty of a Job Schedule
    • Leetcode 1216 Valid Palindrome III
    • Leetcode 97 Interleaving String
    • Leetcode 472 Concatenated Words
    • Leetcode 403 Frog Jump
    • Leetcode 329 Longest Increasing Path in a Matrix

前缀和(Prefix Sum)

  • 基础知识:前缀和本质上是在一个list当中,用O(N)的时间提前算好从第0个数字到第i个数字之和,在后续使用中可以在O(1)时间内计算出第i到第j个数字之和,一般很少单独作为一道题出现,而是很多题目中的用到的一个小技巧
  • 常见题目:
    • Leetcode 53 Maximum Subarray
    • Leetcode 1423 Maximum Points You Can Obtain from Cards
    • Leetcode 1031 Maximum Sum of Two Non-Overlapping Subarrays
    • Leetcode 523 Continuous Subarray Sum
    • Leetcode 304 Range Sum Query 2D - Immutable

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/69359.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu文件同步

1. 使用 rsync 同步文件 rsync 是一个常用的文件同步工具,可以在本地或远程系统之间同步文件和目录。 基本用法: rsync -avz /源目录/ 目标目录/-a:归档模式,保留文件属性。-v:显示详细输出。-z:压缩传输…

在CT107D单片机综合训练平台上,8个数码管分别单独依次显示0~9的值,然后所有数码管一起同时显示0~F的值,如此往复。

题目:在CT107D单片机综合训练平台上,8个数码管分别单独依次显示0~9的值,然后所有数码管一起同时显示0~F的值,如此往复。 延时函数分析LED首先实现8个数码管单独依次显示0~9的数字所有数码管一起同时显示0~F的值,如此往…

详解Swift中 Sendable AnyActor Actor GlobalActor MainActor Task、await、async

详解Swift中 Sendable AnyActor Actor GlobalActor MainActor 的关联或者关系 及其 各自的作用 和 用法 以及与 Task、await、async: Sendable 协议 作用: Sendable 是一个协议,它用于标记可以安全地跨线程或异步任务传递的数据类型。符合 S…

使用VCS对Verilog/System Verilog进行单步调试的步骤

Verilog单步调试: System Verilog进行单步调试的步骤如下: 1. 编译设计 使用-debug_all或-debug_pp选项编译设计,生成调试信息。 我的4个文件: 1.led.v module led(input clk,input rst_n,output reg led );reg [7:0] cnt;alwa…

Kotlin 2.1.0 入门教程(十)if、when

if 表达式 if 是一个表达式&#xff0c;它会返回一个值。 不存在三元运算符&#xff08;condition ? then : else&#xff09;&#xff0c;因为 if 在这种场景下完全可以胜任。 var max aif (a < b) max bif (a > b) {max a } else {max b }max if (a > b) a…

数据结构及排序算法

数据结构 线性结构 ◆线性结构:每个元素最多只有一个出度和一个入度,表现为一条线状。线性表按存储方式分为顺序表和链表。 存储结构: ◆顺序存储:用一组地址连续的存储单元依次存储线性表中的数据元素,使得逻辑上相邻的元素物理上也相邻。 ◆链式存储:存储各数据元素的结点…

camera系统之cameraprovider

在相机系统中&#xff0c;CameraProvider是一个至关重要的组件。以下是对CameraProvider的详细解释&#xff1a; 一、定义与位置 CameraProvider是相机系统的下层组件&#xff0c;位于CameraService以下和Camera HAL&#xff08;硬件抽象层&#xff09;以上。它作为一个独立进…

python实现多路视频,多窗口播放功能

系列Python开发 文章目录 系列Python开发前言一、python实现多路视频播放功能二、代码实现1. http申请视频流地址并cv2播放功能 三、打包代码实现生成可执行文件 总结 前言 一、python实现多路视频播放功能 服务端开发后通常需要做功能测试、性能测试&#xff0c;通常postman、…

【R语言】数据操作

一、查看和编辑数据 1、查看数据 直接打印到控制台 x <- data.frame(a1:20, b21:30) x View()函数 此函数可以将数据以电子表格的形式进行展示。 用reshape2包中的tips进行举例&#xff1a; library("reshape2") View(tips) head()函数 查看前几行数据&…

51单片机之使用Keil uVision5创建工程以及使用stc-isp进行程序烧录步骤

一、Keil uVision5创建工程步骤 1.点击项目&#xff0c;新建 2.新建目录 3.选择目标机器&#xff0c;直接搜索at89c52选择&#xff0c;然后点击OK 4.是否添加起吊文件&#xff0c;一般选择否 5.再新建的项目工程中添加文件 6.选择C文件 7.在C文件中右键&#xff0c;添加…

STM32 软件SPI读写W25Q64

接线图 功能函数 //写SS函数 void My_W_SS(uint8_t BitValue) {GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)BitValue); }//写SCK函数 void My_W_SCK(uint8_t BitValue) {GPIO_WriteBit(GPIOA, GPIO_Pin_5, (BitAction)BitValue); }//写MOSI函数 void My_W_MOSI(uint8_t Bit…

apachePoi中XSSFClientAnchor图片坐标简述;填充多张图片

概述 业务中经常会遇到在单元格内填充图片的需求&#xff0c;而且要求指定图片在单元格内的位置。 一般都是用的apache的poi&#xff0c;设置图片坐标。 HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)dx1 dy1 起始单元…

Centos挂载镜像制作本地yum源,并补装图形界面

内网环境centos7.9安装图形页面内网环境制作本地yum源 上传镜像到服务器目录 创建目录并挂载镜像 #创建目录 cd /mnt/ mkdir iso#挂载 mount -o loop ./CentOS-7-x86_64-DVD-2009.iso ./iso #前面镜像所在目录&#xff0c;后面所挂载得目录#检查 [rootlocalhost mnt]# df -h…

百科词条创建审核不通过的原因有哪些?

我们知道的国内有名的百科网站有百度百科、快懂百科、搜狗百科、360百科,这些有名的百科网站。一般来说,百科的词条排名都是在第一页的,无论是名人、明星、软件、影视名称,还是其他名称,大多排名都在首页,这就拥有了更多的曝光量和流量,而且由于百科是人们获取信息、查找资料的…

ssti学习笔记(服务器端模板注入)

目录 一&#xff0c;ssti是什么 二&#xff0c;原理 所谓模板引擎&#xff08;三列&#xff0c;可滑动查看&#xff09; 三&#xff0c;漏洞复现 1&#xff0c;如何判断其所属的模板引擎&#xff1f; 2&#xff0c;判断清楚后开始注入 &#xff08;1&#xff09;Jinja2&a…

【前端】Python 闭包与JavaScript闭包的实现差异

目录 Python 闭包JavaScript 闭包 推荐超级课程&#xff1a; Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战 Python 闭包 如何定义&#xff1a; 在一个函数内部定义另一个函数&#xff0c;内部函数引用外部函数的变量。 def outer_function(text):…

【JVM详解二】常量池

一、常量池概述 JVM的常量池主要有以下几种&#xff1a; class文件常量池运行时常量池字符串常量池基本类型包装类常量池 它们相互之间关系大致如下图所示&#xff1a; 每个 class 的字节码文件中都有一个常量池&#xff0c;里面是编译后即知的该 class 会用到的字面量与符号引…

人工智能入门 数学基础 线性代数 笔记

必备的数学知识是理解人工智能不可或缺的要素&#xff0c;今天的种种人工智能技术归根到底都建立在数学模型之上&#xff0c;而这些数学模型又都离不开线性代数&#xff08;linear algebra&#xff09;的理论框架。 线性代数的核心意义&#xff1a;世间万事万物都可以被抽象成某…

C# Winform怎么设计串口,客户端和相机控件界面显示

首先我们必须把这个类创建好 INIAPI using System; using System.Text; using System.Runtime.InteropServices;namespace Ini {public class IniAPI{#region INI文件操作/** 针对INI文件的API操作方法&#xff0c;其中的节点&#xff08;Section)、键&#xff08;KEY&#x…

【大数据技术】用户行为日志分析(python+hadoop+mapreduce+yarn+hive)

用户行为日志分析(python+hadoop+mapreduce+yarn+hive) 搭建完全分布式高可用大数据集群(VMware+CentOS+FinalShell) 搭建完全分布式高可用大数据集群(Hadoop+MapReduce+Yarn) 本机PyCharm远程连接虚拟机Python 搭建完全分布式高可用大数据集群(MySQL+Hive)