
LeetCode
全都是泡饃
赖床一时爽,一直赖床一直爽
展开
-
Leetcode43:接雨水(动态规划)
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]输入:height = [4,2,0,3,2,5]...原创 2022-08-29 17:47:21 · 538 阅读 · 1 评论 -
滑动窗口模板(leetcode76 最小覆盖子串)
给你一个字符串 s 、一个字符串 t。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""。对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。输入:s = "ADOBECODEBANC", t = "ABC"解释: t 中两个字符 'a' 均应包含在 s 的子串中,如果 s 中存在这样的子串,我们保证它是唯一的答案。输入: s = "a", t = "aa"输入:s = "a", t = "a"...原创 2022-08-28 21:53:35 · 232 阅读 · 0 评论 -
剑指 Offer 10- II. 青蛙跳台阶问题(矩阵快速幂)
class Solution {public: typedef vector<vector<long>> vvl; vvl multiply(vvl& a,vvl& b){ vvl c={{1,0},{0,1}}; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ c[i][j]=(a[i][...原创 2022-02-19 21:02:13 · 626 阅读 · 2 评论 -
剑指Offer004:只出现一次的数字
方法一:哈希表,需要额外空间class Solution {public: int singleNumber(vector<int>& nums) { unordered_map<int,int> hash_map; for(auto& num:nums){ hash_map[num]++; } for(auto& [val,count]:hash_ma..原创 2022-02-19 13:41:25 · 129 阅读 · 0 评论 -
剑指Offer003:前 n 个数字二进制中 1 的个数
方法一:bitset容器class Solution {public: vector<int> countBits(int n) { vector<int> ans; for(int i=0;i<=n;i++){ bitset<32> bs(i); ans.push_back((int)bs.count()); } return ans..原创 2022-02-19 13:16:06 · 385 阅读 · 0 评论 -
剑指Offer002:二进制加法
class Solution {public: string addBinary(string a, string b) { int idx_a=a.size()-1,idx_b=b.size()-1; string ans=""; int carry=0;//进位初始化为0 while(idx_a>=0||idx_b>=0||carry!=0){ int sum=carry; ...原创 2022-02-19 12:15:39 · 7545 阅读 · 0 评论 -
剑指Offer001:整数除法
提示:-231<= a, b <= 231- 1 b != 0class Solution {public: int divide(int a, int b) { bool sign=(a^b)>>31;//判断a和b是否异号,右移31位到符号位 long dividend=abs((long)a),divisor=abs((long)b);//转为正数 long ans=0; while(d...原创 2022-02-19 12:11:31 · 323 阅读 · 0 评论 -
Leetcode688:骑士在棋盘上的概率(动态规划)
On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).A chess knight has eight possible move原创 2022-02-17 13:19:28 · 492 阅读 · 0 评论 -
Leetcode1719:重构一棵树的方案数(拓扑)
You are given an array pairs, where pairs[i] = [xi, yi], and:There are no duplicates.xi < yiLet ways be the number of rooted trees that satisfy the following conditions:The tree consists of nodes whose values appeared in pairs.A pair [xi, yi] exi原创 2022-02-16 15:08:11 · 336 阅读 · 0 评论 -
Leetcode297:二叉树的序列化与反序列化(广搜)
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer env原创 2022-02-15 20:14:12 · 501 阅读 · 0 评论 -
Leetcode236:二叉树的最近公共祖先(广搜)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as d原创 2022-02-14 21:33:08 · 92 阅读 · 0 评论 -
Leetcode116:填充每个节点的下一个右侧节点指针(层序遍历)
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; Node *right; Node *next;}Populate each next pointer t...原创 2022-02-12 16:58:57 · 238 阅读 · 0 评论 -
Leetcode105:从前序与中序遍历序列构造二叉树(哈希表+递归)
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.Constraints:1 <= preorder.length <= 3000inorder.l原创 2022-02-12 15:55:29 · 212 阅读 · 0 评论 -
Leetcode106:从中序与后序遍历序列构造二叉树(哈希表+递归)
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.Constraints:1 <= inorder.length <= 3000posto原创 2022-02-12 15:37:16 · 350 阅读 · 0 评论 -
Leetcode1020:飞地的数量(广搜)
You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.Return the numbe原创 2022-02-12 14:44:57 · 329 阅读 · 0 评论 -
Leetcode1984:学生分数的最小差值(排序+划窗)
You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k.Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k score原创 2022-02-11 11:22:10 · 528 阅读 · 0 评论 -
Leetcode2006:差的绝对值为K的数对数目(哈希表)
Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.The value of |x| is defined as:x if x >= 0.-x if x < 0.Constraints:1 <= nums.length <= 200 1 <= num原创 2022-02-09 12:11:39 · 5987 阅读 · 0 评论 -
Leetcode1405:最长快乐字符串(贪心)
A string s is called happy if it satisfies the following conditions:s only contains the letters 'a', 'b', and 'c'.s does not contain any of "aaa", "bbb", or "ccc" as a substring.s contains at most a occurrences of the letter 'a'.s contains at most b o原创 2022-02-07 17:39:23 · 616 阅读 · 0 评论 -
Leetcode1748:唯一元素的和(哈希表)
You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.Return the sum of all the unique elements of nums.Constraints:1 <= nums.length <= 100 1 <= nums[i] <= 100clas原创 2022-02-06 17:30:11 · 348 阅读 · 0 评论 -
Leetcode1219:黄金矿工(DFS+lambda表达式)
In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.Return the maximum amount of gold you can collect under the conditions:Every time you are located in a cell you will原创 2022-02-05 12:11:32 · 1566 阅读 · 0 评论 -
Leetcode1414:和为K的最小斐波拉契数字数目(贪心+二分)
Given an integer k, return the minimum number of Fibonacci numbers whose sum is equal to k. The same Fibonacci number can be used multiple times.The Fibonacci numbers are defined as:F1 = 1F2 = 1Fn = Fn-1 + Fn-2 for n > 2.It is guaranteed that for原创 2022-02-03 16:07:07 · 777 阅读 · 0 评论 -
Leetcode1349:参加考试的最大学生数(动态规划+状态压缩+位运算)
Given a m * n matrix seats that represent seats distributions in a classroom. If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.Students can see the answers of those sitting next to the left, right, upper left原创 2022-02-02 17:22:18 · 624 阅读 · 0 评论 -
巧用C++位运算
C++位运算可以提高运算效率移位:用x<<1代替x*2;x>>1代替x/2位与:取余运算x%n可用x&(n-1)代替原创 2021-12-04 11:41:01 · 317 阅读 · 0 评论 -
[leetcode C++] Remove Element 双指针法
Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.The order of element原创 2021-02-19 21:29:57 · 167 阅读 · 0 评论