leetcode tarjan 341 lca

Template <lca 最近公共祖先>

# 01 倍增lca [P3379 【模板】最近公共祖先(LCA) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)](https://www.luogu.com.cn/problem/P3379) ## 1.1 常用简短版本(利用结点深度) ```cpp #include #inc ......
祖先 Template lca lt gt

leetcode647. 回文子串

``` class Solution { public: int res=0; int countSubstrings(string s) { int n=s.size(); for(int i=0;i=0&&r=0&&r<n&&s[l]==s[r]) l--,r++,res++; } return ......
回文 leetcode 647

最近公共祖先(LCA)

> 「观前提醒」 > > 「文章仅供学习和参考,如有问题请在评论区提出」 [toc] ## 前言 简单的模板整理,只是概括了一下具体的实现方法(说到底是给自己写的),如果看不明白可以去看原视频(讲的很好),链接在参考资料里。 ## 定义 **最近公共祖先**简称 $LCA$(`Lowest Comm ......
祖先 LCA

LeetCode从算法到算命——每日一题(0802)

LeetCode从算法到算命—每日一题(0802) # 822. 翻转卡片游戏 ## 题目信息 在桌子上有 n 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样)。 我们可以先翻转任意张卡片,然后选择其中一张卡片。 如果选中的那张卡片背面的数字 x 与任意一张卡片的正面的数字 ......
算法 LeetCode 0802

LeetCode 热题 100 之 73. 矩阵置零

# 题目 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 **示例 1:** 输入:matrix = [[1,1,1],[1,0,1],[1,1,1]] 输出:[[1,0,1],[0,0,0],[1,0,1]] **示例 2:** 输 ......
矩阵 LeetCode 100 73

(*)LeetCode 热题 100 之 238. 除自身以外数组的乘积

# 题目 给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请**不要使用除法**,**且在 O(n) 时 ......
乘积 数组 LeetCode 100 238

LeetCode 热题 100 之 189. 轮转数组

# 题目 给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 **示例 1:** 输入: nums = [1,2,3,4,5,6,7], k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步 ......
数组 LeetCode 100 189

LCA

[toc] # LCA最近公共祖先 最近公共祖先简称 LCA(Lowest Common Ancestor)。两个节点的最近公共祖先,就是这两个点的公共祖先里面,离根最远的那个。 暴力求解太慢,这里先摘记一种做法-**倍增法**。 对于每一个节点,我们先通过**dfs预先处理**出当前节点向根移动2 ......
LCA

代码随想录算法训练营第七天| LeetCode 454.四数相加II 15. 三数之和 18. 四数之和

454.四数相加II 卡哥建议:本题是使用map巧妙解决的问题,好好体会一下 哈希法如何提高程序执行效率,降低时间复杂度,当然使用哈希法会提高空间复杂度,但一般来说我们都是舍空间换时间, 工业开发也是这样。 题目链接/文章讲解/视频讲解:https://programmercarl.com/0454 ......
之和 随想录 训练营 随想 算法

LeetCode 周赛上分之旅 # 36 KMP 字符串匹配殊途同归

> ⭐️ **本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 \[彭旭锐] 和 [BaguTree Pro](https://files.mdnice.com/user/3257/de950859-eb71-4821-a36b-bebe5cff500d.png) 知识星球提问 ......

luogu P4592 [TJOI2018] 异或 题解【可持久化01trie+LCA+dfs序】

[TOC] # 题目链接 [P4592 [TJOI2018] 异或](https://www.luogu.com.cn/problem/P4592) # 解题思路 读完题目首先发现很像最大异或和问题 但是在树上操作 一开始想到树剖 但是树剖有两个 $\log$ ~~但是树剖常数小~~ 考虑`dfs` ......
题解 luogu P4592 4592 2018

LeetCode 581. 最短无序连续子数组

``` class Solution { public: int findUnsortedSubarray(vector& nums) { int n=nums.size(); int l=0,r=n-1; while(l0&&nums[r]>=nums[r-1]) r--; int min_num ......
数组 LeetCode 581

第 356 场周赛 - 力扣(LeetCode)

# [第 356 场周赛 - 力扣(LeetCode)](https://leetcode.cn/contest/weekly-contest-356/) ## [2798. 满足目标工作时长的员工数目 - 力扣(LeetCode)](https://leetcode.cn/problems/num ......
LeetCode 356

LeetCode 543. 二叉树的直径

``` /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), ......
直径 LeetCode 543

[LeetCode] 712. Minimum ASCII Delete Sum for Two Strings

Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal. Example 1: Input: s1 = "sea", s2 = "eat" Out ......
LeetCode Minimum Strings Delete ASCII

代码随想录算法训练营第三天| LeetCode 242.有效的字母异位词 349. 两个数组的交集 1. 两数之和

242.有效的字母异位词 卡哥建议: 这道题目,大家可以感受到数组用来做哈希表给我们带来的遍历之处。 题目链接/文章讲解/视频讲解: https://programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E ......
随想录 之和 训练营 数组 交集

leetcode-n-sum总结

总结一下leetcode中遇见的2-sum, 3-sum, 4-sum问题,并扩展到n-sum。 1. 两数之和 - 力扣(LeetCode) 梦开始的地方,不多说。 class Solution { public int[] twoSum(int[] nums, int target) { Map ......
leetcode-n-sum leetcode sum

LeetCode/课程表IV

你总共需要上 numCourses 门课,课程编号依次为 0 到 numCourses-1 。你会得到一个数组 prerequisite ,其中 prerequisites[i] = [ai, bi] 表示如果你想选 bi 课程,你 必须 先选 ai 课程。 有的课会有直接的先修课程,比如如果想上课 ......
课程表 LeetCode 课程

模板 最近公共祖先(LCA)

```c++ void dfs(int u, int v) {//求出每个结点的深度1 dep[u] = dep[p] + 1; fa[u][0] = p; for(int i = 1; (1 = 0; i--) {//i的初始值由节点数确定 if(dep[u] - (1 = dep[v]) { u ......
祖先 模板 LCA

leetcode-异位词问题总结

总结一下leetcode中遇见的异位词问题: 242. 有效的字母异位词 - 力扣(LeetCode) 本题是异位词题目中最基础的,有两种方法可以轻松解决: 1. 排序法,时间复杂度O(n log n): class Solution { //排序解决 public boolean isAnagra ......
leetcode 问题

leetcode第353场周赛 4 - 差分数组维护区间修改

[题目传送门](https://leetcode.cn/contest/weekly-contest-353/) # [2772. 使数组中的所有元素都等于零](https://leetcode.cn/problems/apply-operations-to-make-all-array-eleme ......
数组 区间 leetcode 353

代码随想录算法训练营第四天| LeetCode 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 142.环形链表II

24. 两两交换链表中的节点 卡哥建议:用虚拟头结点,这样会方便很多。 本题链表操作就比较复杂了,建议大家先看视频,视频里我讲解了注意事项,为什么需要temp保存临时节点。 题目链接/文章讲解/视频讲解:https://programmercarl.com/0024.%E4%B8%A4%E4%B8% ......
节点 随想录 环形 训练营 随想

LeetCode做题笔记

LeetCode做题笔记 3. 无重复字符的最长子串 最核心的就是维护一个字符串区间,同时保证其中的每个字符都只出现过一次。最最最暴力的就是枚举可能出现的每个字符串区间,然后确保里边元素不重复,然后找出最长的字符串有多少元素。再进一步的想法就是由小到大的构建这样的字符串。用双指针滑动窗口的思想来维护 ......
LeetCode 笔记

LeetCode 热题 100 之 56. 合并区间

# 题目 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 **示例 1:** 输入:intervals = [[1,3],[2, ......
区间 LeetCode 100 56

Leetcode448. 找到所有数组中消失的数字

``` class Solution { public: vector findDisappearedNumbers(vector& nums) { vector res; int n=nums.size(),offset=n+1; for(auto &num:nums) { int x=(num) ......
数组 Leetcode 数字 448

[LeetCode] 486. Predict the Winner

You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2. Player 1 and player 2 take turns, with pla ......
LeetCode Predict Winner 486 the

代码随想录算法训练营第三天| LeetCode 203.移除链表元素(同时也对整个单链表进行增删改查操作) 707.设计链表 206.反转链表

203.移除链表元素 题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html 卡哥题目建议:本题最关键是要理解虚拟头结点的使用技巧,这个对链 ......
随想录 训练营 随想 算法 LeetCode

LeetCode 239. Sliding Window Maximum 单调队列

You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. Yo ......
队列 LeetCode Sliding Maximum Window

Leetcode438. 找到字符串中所有字母异位词

``` class Solution { public: vector findAnagrams(string s, string p) { unordered_map window,hash; vector res; for(auto i:p) hash[i]++; for(int i=0,j=0 ......
字符串 字母 字符 Leetcode 438

代码随想录算法训练营第二天| LeetCode 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

977.有序数组的平方 题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/ 文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7% ......
数组 随想录 训练营 矩阵 螺旋