LeetCode 题目总结 本博客不定期更新 LeetCode 题目总结,所有题目使用 Java 实现,小部分题目也提供 JavaScript 代码。我不追求一行 AC 但是我追求一题多解,比较常规的思路,解释清楚复杂度,代码可读性强。欢迎留言和评论,共同进步。这本是我自己用来复习的笔记,如果也能帮到你,那也是我的福报。 如果你想按类型刷题,可以参考我的标签。我做出的分类比 LC 官方的更细一些,比如我有如下展示的这些类型, 2020-03-18 #leetcode
[LeetCode] 二分查找模板 binary search 二分法是算法题里面一个比较基础但是很容易错的概念,一开始练习的时候由于不熟悉二分法的套路,反复出现死循环或者目标值找错,非常影响做题心情。我总结了如下几个模板。原则上这里的模板无论你使用哪一个,都可以解决二分法类型的问题,只不过有一些题目,比如寻找一个最大值/最小值的,可能某一个模板更适合,需要判断的条件较少。 如下模板是用Java实现的 模板一,找有序数组中是否存在一个目标值。注意 r 2021-01-12 #leetcode #java #binary search
[LeetCode] 3075. Maximize Happiness of Selected Children You are given an array happiness of length n, and a positive integer k. There are n children standing in a queue, where the ith child has happiness value happiness[i]. You want to select k children fr 2024-05-09 #leetcode #array #java #greedy #sort
[LeetCode] 2960. Count Tested Devices After Test Operations You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices. Your task is to test each device i in order from 0 to n - 1, by per 2024-05-09 #leetcode #array #java #simulation
[LeetCode] 2105. Watering Plants II Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs 2024-05-09 #leetcode #array #two pointer #java #simulation
[LeetCode] 2079. Watering Plants You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There i 2024-05-07 #leetcode #array #java #greedy #simulation
[LeetCode] 2487. Remove Nodes From Linked List You are given the head of a linked list. Remove every node which has a node with a greater value anywhere to the right side of it. Return the head of the modified linked list. Example 1:Input: head 2024-05-06 #leetcode #java #stack #linked list #recursion #monotonic stack
[LeetCode] 2739. Total Distance Traveled A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in lit 2024-04-24 #leetcode #java #math
[LeetCode] 2073. Time Needed to Buy Tickets There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line. You are given a 0-indexed integer array ticke 2024-04-09 #leetcode #array #java #simulation
[LeetCode] 2192. All Ancestors of a Node in a Directed Acyclic Graph You are given a positive integer n representing the number of nodes of a Directed Acyclic Graph (DAG). The nodes are numbered from 0 to n - 1 (inclusive). You are also given a 2D integer array edges, 2024-04-06 #leetcode #java #bfs #dfs #graph #topological sort