LeetCode 0051】【剪枝】N皇后

N-Queens The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. You...

Leetcode刷题4--- 寻找两个正序数组的中位数 Python

目录 题目及分析方法一:直接合并后排序方法二:二分查找法 题目及分析 (力扣序号4:[寻找两个正序数组的中位数](https://leetcode.cn/problems/median-of-two-sorted-arrays/description/) 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 示例 1: 输入:nums1 ...

leetcode-动态规划-01背包

一、二维数组 1、状态转移方程: 不放物品i:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以背包内的价值依然和前面相同。)放物品i:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weigh...

LeetCode 0102】【BSF】二叉树的层级遍历

Binary Tree Level Order Traversal Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level). Example 1: **Input:** root = [3,9,20,null...

LeetCode 0088】 【数组/双指针】合并两个有序数组

Merge Sorted Array You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1...

LeetCode.面试题17.24.最大子矩阵详解

问题描述 给定一个正整数、负整数和 0 组成的 N × M 矩阵,编写代码找出元素总和最大的子矩阵。 返回一个数组 [r1, c1, r2, c2],其中 r1, c1 分别代表子矩阵左上角的行号和列号,r2, c2 分别代表右下角的行号和列号。若有多个满足条件的子矩阵,返回任意一个均可。 解题思路 1. 基础概念:Kadane算法 首先,我们需要了解Kadane算法,这是一种用于在一维数组中找到最大子数组和的算...

【哈希表】【字符串】个人练习-Leetcode-1814. Count Nice Pairs in an Array

题目链接:https://leetcode.cn/problems/count-nice-pairs-in-an-array/description/ 题目大意:给出一个数列nums[],求nice对 ( i , j ) (i, j) (i,j)对数。nice对满足0 <= i < j < nums.length和nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]),...

【遍历链表】个人练习-Leetcode-LCR 029. 循环有序列表的插入

题目链接:https://leetcode.cn/problems/4ueAj6/description/ 题目大意:给出一个循环链表中间的某个结点的指针head(这个并非真正的头),这个链表从头到尾是非递减的,唯一可能出现递减的地方是【尾部连回头部】处。现在给一个值insertVal,要求将该值插入链表中,保持非递减性质不变。返回的还是原来的head指针。 思路:(1)先考虑正常的从头到尾非递减的情况,如果插...

LeetCode 0225】【队列】用队列实现栈

描述 void push(int x) Pushes element x to the top of the stack.int pop() Removes the element on the top of the stack and returns it.int top() Returns the element on the top of the stack.boolean empty() Returns...

【快慢指针】个人练习-Leetcode-142. Linked List Cycle II

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/ 题目大意:给一个链表的头部,判断链表是否有环,如果有,返回环的第一个指针;如果没有,返回nullptr 思路:简单的思路是并查集,第二次插入的那个指针就是环的起点。但这样空间复杂度还是 O ( N ) O(N) O(N)。使用快慢指针可以让空间复杂度降为 O ( 1 ) O(1...
© 2024 LMLPHP 关于我们 联系我们 友情链接 耗时0.003347(s)
2024-10-23 03:09:45 1729624185