leetcode tarjan 341 lca
LeetCode 85. Maximal Rectangle
class Solution { public: int method(vector<int> h)//求柱状图中最大的矩形 { int n=h.size(); vector<int> l=vector<int> (n),r=l; stack<int> st; //预处理l,r数组 for(int ......
leetcode876. 链表的中间结点
876. 链表的中间结点 方法一: 最简单的做法,先求出整个链表的长度,再求1/2处节点的位置。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode( ......
Leetcode Practice --- 栈和队列
155. 最小栈 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 实现 MinStack 类: MinStack() 初始化堆栈对象。 void push(int val) 将元素val推入堆栈。 void pop() 删除堆栈顶部的元素。 int top() ......
leetcode-733-easy
Flood Fill An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three ......
leetcode-1005-easy
Maximize Sum Of Array After K Negations Given an integer array nums and an integer k, modify the array in the following way: choose an index i and rep ......
leetcode-724-easy
Find Pivot Index Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbe ......
leetcode-762-easy
Prime Number of Set Bits in Binary Representation Given two integers left and right, return the count of numbers in the inclusive range [left, right] ......
leetcode-744-easy
Find Smallest Letter Greater Than Target You are given an array of characters letters that is sorted in non-decreasing order, and a character target. ......
LeetCode 100 相同的树
LeetCode | 100.相同的树 给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1: 输入:p = [1,2,3], q = [1,2,3] 输出:true 示例 2: 输入:p = [1, ......
LeetCode 94 二叉树的中序遍历
LeetCode | 94.二叉树的中序遍历 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 示例 2: 输入:root = [] 输出:[] 示例 3: 输入:root = [1] 输出:[1] 提示: ......
反转链表-leetcode92
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。 示例 1: 输入:head = [1,2,3,4,5], left = 2, right = 4 输出:[1,4 ......
LeetCode 2367 算术三元组的数目
给你一个下标从 0 开始、严格递增 的整数数组 nums 和一个正整数 diff 。如果满足下述全部条件,则三元组 (i, j, k) 就是一个 算术三元组 : i < j < k , nums[j] - nums[i] == diff 且 nums[k] - nums[j] == diff 返回不 ......
【DP】LeetCode 62. 不同路径
题目链接 62. 不同路径 思路 代码 class Solution { public int uniquePaths(int m, int n) { int[][] dp = new int[m][n]; Arrays.fill(dp[0], 1); for(int i = 0; i < m; i ......
159.341 解析器开发难点
159.341 – 2023 Semester 1 Massey UniversityAssignment 1Deadline: Hand in by 5pm on Friday 31st March 2023Evaluation: 10 marks – which is 10% of your f ......
leetcode top100-01
最好能说明一下为什么不怕重复。看评论里有很多人提出这个问题,说hash冲突。 我在这里解答一下这个问题。 1.每次写入时,判断条件 不是当前的key本身存不存在,而是key和 tag 之间的差值存不存在,这一点很重要。 2.题目命题说明了一点,假定只有一个解。也就是说重复元素再多都无所谓。 case ......
leetcode top100 - 02
坑 转换成数字进行运算,最后转换成链表。可能会出现溢出的情况。 因为无论是int还是long类型表达的数字长度是有限的,而链表的长度是可以无限追加的。 解释是干扰你的,其实就是依次从低位到高位的进位过程 笔试思路 把链表依次填充到数组中,数组容易操作,然后逐位进行加法运算; 面试思路 使用链表的思路 ......
Leetcode19. 删除链表的倒数第 N 个结点
19. 删除链表的倒数第 N 个结点 自己纯手写第一题,递归有点冗杂,开辟了虚拟头节点,而且要特别注意边界条件(当倒数第n个正好是头节点时)。 ** * Definition for singly-linked list. * struct ListNode { * int val; * ListN ......
代码随想录Day16-Leetcode104. 二叉树的最大深度,111.二叉树的最小深度 ,222.完全二叉树的节点个数
104. 二叉树的最大深度 首先是层序遍历 /** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val undefined ? 0 : val) * this.l ......
LeetCode 287. 寻找重复数
LeetCode 287. 寻找重复数 题目 \287. 寻找重复数 中等 2.1K 相关企业 给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。 假设 nums 只有 一个重复的整数 ,返回 这个重复的数 。 你设 ......
代码随想录Day15-Leetcode102. 二叉树的层序遍历,226.翻转二叉树,101. 对称二叉树
102. 二叉树的层序遍历 题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/ bfs,队列,记录下本层的数量和下一层的数量 /** * Definition for a binary tree node. * f ......
【DP】LeetCode 剑指 Offer 60. n个骰子的点数
题目链接 剑指 Offer 60. n个骰子的点数 思路 动态规划问题中,只用考虑第 n 个阶段如何由第 n-1 个阶段转化过来 在本题中,就是投掷 n 个骰子的结果如何由 投掷 n-1 个骰子的结果转化过来。 代码 class Solution { public double[] dicesPro ......
[LNOI2014] LCA 树链剖分+离线处理+lca转化
困困的开始了我的修炼树剖之旅途 考虑怎么搞这个lca 是说,习惯了倍增求lca,突然冒出这么一个东西还真不会搞 那要么能一次性求很多个lca(?),要么把deep[lca(i,z)]这个东西转化一下 当我们不会倍增求lca的时候,有一个很朴素的想法就是把x到根节点一路上的点都染色 然后让y节点开始往 ......
leetcode-1089-easy
Duplicate Zeros Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that ele ......
leetcode-1009-easy
Complement of Base 10 Integer The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binar ......
leetcode-1317-easy
Convert Integer to the Sum of Two No-Zero Integers No-Zero integer is a positive integer that does not contain any 0 in its decimal representation. Gi ......
【LeetCode】35.搜索插入位置
题目描述 解法 思路:二分查找 class Solution { public: int searchInsert(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; int count = 0; if(nu ......
【LeetCode】278.第一个错误的版本
题目描述 解法 思路:二分查找 注意:当第一个 isBadVersion(mid)的结果为true时,得到第一个错误的版本 // The API isBadVersion is defined for you. // bool isBadVersion(int version); class Sol ......
【LeetCode】704.二分查找
题目描述 解法 class Solution { public: int search(vector<int>& nums, int target) { int left = 0; int right = nums.size()-1; while(left <= right){ int mid = ......
leetcode 176
leetcode 176 第二高的薪水,查第二高的人的信息 1、使用ifnull(exp1, exp2)函数,limit offset子句 select ifnull( (select distinct salary from Employee order by salary desc limit ......