leetcode dfs 110
Leetcode1~10题整理
1. 两数之和 哈希表:O(n) class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> hs; int n = nums.size(); for(int ......
LeetCode 链表操作
21. 合并两个有序链表 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this ......
12.石油储备(DFS/BFS 统计连通块个数)
石油储备 题目 一片土地可以看作是一个 $n$ 行 $m$ 列的方格矩阵。其中一些方格藏有石油,用 @ 表示,其余方格没有石油,用 * 表示。 每个方格都与其上、下、左、右、左上、右上、左下、右下八个方格视为相邻。 如果两个藏有石油的方格相邻,则它们被认为是处于同一片油田,否则它们被认为是处于不同油 ......
DFS找环,三色标记
0代表还没访问 1代表正在访问 2代表已经访问完 如果dfs过程中遇到1,则表明找到了环 遇到2则不必继续找,用于剪枝 https://blog.csdn.net/lj12358132134/article/details/80458349 ......
7-009-(LeetCode- 309) 最佳买卖股票时机含冷冻期
1. 题目 读题 考查点 2. 解法 思路 代码逻辑 具体实现 1 1 3. 总结 ......
LeetCode/移动石子直到连续
###1. 移动石子直到连续(三个石子) class Solution { public: vector<int> numMovesStones(int a, int b, int c) { int x = min({a, b, c}); int z = max({a, b, c}); int y ......
[LeetCode] 1033. Moving Stones Until Consecutive
There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones. In one move, you p ......
leetcode_D10_136只出现一次的数字
1.题目 2.解一 leetcode官方解法,主要用到了两个知识:第一个是reduce函数,需要注意的是在python3中,需要 通过“ from functools import reduce ”从模块中调用该函数;第二个知识是位运算中的异或运算,主要有以下计算法则。 有了这两个补充知识,该题目就 ......
2023-04-28 LeetCode精选题目20道附我的Java实现
LeetCode精选题目20道 1.56.合并区间 贪心 给出一个区间的集合,请合并所有重叠的区间。 示例 1: 输入: intervals = [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] 和 [2,6] ......
C语言刷leetcode——并查集
概述 https://leetcode.cn/problems/number-of-provinces/solution/python-duo-tu-xiang-jie-bing-cha-ji-by-m-vjdr/ 基本概念 并查集是一种数据结构 并查集这三个字,一个字代表一个意思。 并(Union ......
vscode-leetcode
vscode 里写 leetcode 需要的插件xavier-cai.vscode-leetcode-cpp-debug, leetcode.vscode-leetcode LeetCode C++ Debugger.Delete Temporary Contents置为false ctrl+shi ......
LeetCode 241 为运算表达式设计优先级
LeetCode | 241.为运算表达式设计优先级 给你一个由数字和运算符组成的字符串 expression ,按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以 按任意顺序 返回答案。 生成的测试用例满足其对应输出值符合 32 位整数范围,不同结果的数量不超过 104 。 示例 ......
【二分查找】LeetCode 153. 寻找旋转排序数组中的最小值
题目链接 153. 寻找旋转排序数组中的最小值 思路 首先分析一下旋转数组可能有的状态: 左 < 中 < 右,此时最小值肯定在左边,应当收缩右边界 左 < 中,中 > 右,此时最小值肯定在右半段,应当收缩左边界 左 > 中,中 < 右,此时最小值肯定在左半段,应当收缩右边界 分析这三种状态可以发现, ......
[LeetCode] 1031. Maximum Sum of Two Non-Overlapping Subarrays
Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths ......
LeetCode 双指针
15. 三数之和 (为0) 下标不能是重复的,必定右 i<l<r 1、先对数组排序(从小到大) 2、外层 i 遍历 如果 nums[i] > 0 ,整个 nums[] 后面的必定无法有三元组为0(排过序了,后面的 nums[l] nums[r] 都会大于0)。break。 如果 nums[i] = ......
旅游规划 树形DP DFS
🍑 算法题解专栏 🍑 旅游计划 输入 10 0 1 0 2 0 4 0 6 0 7 1 3 2 5 4 8 6 9 输出 0 1 2 3 4 5 6 8 9 🍑 树形DP:即在树上进行的 DP,由于树固有的递归性质,树形DP一般都是递归进行的。 🍑 树的直径:找到根节点下面的第一长和第二长的 ......