
每日一题
小明的博客
acm小弟
展开
-
Leecode<每日一题>正则表达式匹配
Leecode<每日一题>正则表达式匹配题目链接思路:递归+枚举class Solution {public:bool isMatch(string s, string p) { int num = 0; for (int i = 0; i < p.length(); i++) { if (p[i] == '*') continue; if (i + 1 < p.length() && p[i + 1] == '*') { if (p[i] =原创 2021-08-19 11:22:27 · 288 阅读 · 0 评论 -
Leecode<每日一题>N 皇后
Leecode<每日一题>N 皇后题目链接思路:回溯法+限制数组class Solution {public: bool lie[10]{}; bool up[20]{}; bool down[20]{}; int choose[10]{}; vector<vector<string>> rt; void dfs(int cur,int n) {原创 2021-08-18 10:11:31 · 313 阅读 · 0 评论 -
Leecode<每日一题>学生出勤记录 II
Leecode<每日一题>学生出勤记录 II题目链接思路:动态规划,dp[i][0],表示n == i,缺勤次数为0时的可能情况数。dp[i][1],表示n == i,缺勤次数为1时的可能情况数。class Solution {public: int checkRecord(int n) { long long dp[100010][2]{}; dp[1][0] = 2,dp[1][1] = 1; dp[2][0] = 4,dp[2][1]原创 2021-08-17 10:59:40 · 283 阅读 · 0 评论 -
Leecode<每日一题>优美的排列
Leecode<每日一题>优美的排列题目链接思路:枚举优化法,将每个位置可选择的情况枚举出来,然后通过递归枚举每一种可行的情况。class Solution {public: vector<vector<int>> ab_use = { {}, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {1,2,4,6,8,10,12,14}, {1,3,6,9,12,15}, {1,2,4,8,12}, {1,5,10,15}, {原创 2021-08-16 14:27:45 · 202 阅读 · 0 评论 -
Leecode<每日一题>出界的路径数
Leecode<每日一题>出界的路径数题目链接思路:动态规划,dp[i][j][step] 表示在位置[i,j]处,可移动距离为step时,出界的路径数。class Solution {public:int dp[51][51][51]{};vector<vector<int>> dir= { {1,0},{-1,0},{0,1},{0,-1} }; int findPaths(int m, int n, int maxMove, int startRow, i原创 2021-08-15 15:49:46 · 197 阅读 · 0 评论 -
Leecode<每日一题>统计不开心的朋友
Leecode<每日一题>统计不开心的朋友题目链接思路:先打两张表,一张是匹配表,另一张是两个人之间的好感优先级表,然后枚举就行。class Solution {public: int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) { int match[510][510];原创 2021-08-14 10:21:12 · 184 阅读 · 0 评论 -
Leecode<每日一题>数字 1 的个数
Leecode<每日一题>数字 1 的个数题目链接思路:找规律,统计每一位上1的个数class Solution {public: int countDigitOne(int n) { string num = to_string(n); int sum = 0; int fro = 0; for (int i = 0; i < num.length(); i++) { int yu = (int)pow(10, num.size()-i-1); in原创 2021-08-13 11:22:33 · 239 阅读 · 0 评论 -
Leecode<每日一题>最长回文子序列
Leecode<每日一题>最长回文子序列题目链接思路:动态规划dp[i][j]表示下标i到j的区间内,最大的回文子序列的长度。动态转移方程:dp[i][end] = dp[i + 1][end - 1] + 2; //区间首位相同的转移方程dp[i][end] = max(dp[i][end - 1],dp[i+1][end]); //区间首位不同的转移方程class Solution {public: int longestPalindromeSubseq(string s原创 2021-08-12 10:38:56 · 210 阅读 · 0 评论 -
Leecode<每日一题>等差数列划分 II - 子序列
Leecode<每日一题>等差数列划分 II - 子序列题目链接思路:动态规划+哈希表class Solution {public:typedef long long ll; int numberOfArithmeticSlices(vector<int>& nums) { auto tab = vector<unordered_map<int, int>>(nums.size() + 1); int sum = 0; for (int原创 2021-08-11 10:37:05 · 168 阅读 · 0 评论 -
Leecode<每日一题>环形数组是否存在循环
Leecode<每日一题>环形数组是否存在循环题目链接思路:彩色标记法,同一入口走过的路用同一种颜色标记时间复杂度o(n)空间复杂度o(1)class Solution {public:#define k 1010bool circularArrayLoop(vector<int>& nums) { for (int i = 0; i < nums.size(); i++){ if (nums[i] != 0) { int sign = nums[i]原创 2021-08-07 11:03:01 · 140 阅读 · 0 评论 -
Leecode<每日一题>找到最终的安全状态
Leecode<每日一题>找到最终的安全状态题目链接思路:拓扑排序,先根据原图记录一下反图,然后将原图中出度为0的点加入队列中,然后将以此结点为边的目标点的源结点出度减一,然后再将出度为0的点加入队列中,直至队列为空。vector<int> eventualSafeNodes(vector<vector<int>>& graph) { vector<int> outdu(graph.size()); vector<int> rs原创 2021-08-05 15:02:45 · 137 阅读 · 0 评论 -
Leecode<每日一题> 网络延迟时间
Leecode<每日一题> 网络延迟时间题目链接思路:其实就是求最小生成树,我使用的是prim算法维护两个集合,一个是已添加结点集合,一个是未添加集合,循环n-1次,维护一个距离数组,每次循环添加一个离源点最短的结点进入集合,并以此更新结点的距离。int networkDelayTime(vector<vector<int>>& times, int n, int k) { auto graph = vector<vector<pair<int,i原创 2021-08-02 11:22:53 · 223 阅读 · 0 评论 -
Leecode<每日一题>二叉树的垂序遍历
Leecode<每日一题>二叉树的垂序遍历题目链接思路:map+multiset+dfs这题其实就是按照纵坐标、横坐标、值依次排序。map 键:坐标 值:multiset(用于给同一坐标的值排序),然后重写map比较器,先纵坐标再横坐标我的代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;原创 2021-07-31 10:53:54 · 216 阅读 · 0 评论 -
Leecode<每日一题>二叉树中所有距离为 K 的结点
Leecode<每日一题>二叉树中所有距离为 K 的结点题目链接思路:先遍历一遍将每个节点的父节点存储下来,然后从target开始层次遍历,找到距离为k的节点即可。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),原创 2021-07-28 10:35:15 · 208 阅读 · 0 评论 -
Leecode<每日一题>得到子序列的最少操作次数
Leecode<每日一题>得到子序列的最少操作次数题目链接思路:实际上这题是求公共最长子序列,我是用哈希表+动态规划+二分查找哈希表存target元素所对应的下标dp[i] 表示长度为i的子序列元素的最后位置二分查找每次找到第一个比mp[x]大的元素,并更新他;class Solution {public: int dp[100100]{};int minOperations(vector<int>& target, vector<int>&原创 2021-07-26 11:15:39 · 187 阅读 · 0 评论 -
Leecode<每日一题>替换隐藏数字得到的最晚时间
Leecode<每日一题>替换隐藏数字得到的最晚时间题目链接思路:枚举所有情况class Solution {public: string maximumTime(string time) { if(time[0] == '?' && time[1] == '?') { time[0] = '2'; time[1] = '3'; } if(time[0] == '原创 2021-07-24 10:03:55 · 157 阅读 · 0 评论 -
Leecode<每日一题>寻找重复数
Leecode<每日一题>寻找重复数题目链接思路:快慢指针class Solution {public: int findDuplicate(vector<int>& nums) { int slow=0,fast=0; do { slow = nums[slow]; fast = nums[nums[fast]]; }while(nums[slow] != n原创 2021-07-23 17:11:47 · 210 阅读 · 0 评论 -
Leecode<每日一题>检查是否区域内所有整数都被覆盖
Leecode<每日一题>检查是否区域内所有整数都被覆盖题目链接思路:差分数组class Solution {public: int f[52]{}; bool isCovered(vector<vector<int>>& ranges, int left, int right) { for (auto& x:ranges) { f[x[0]]++; f[x[1] + 1]--; } int sum = 0; for (int i原创 2021-07-23 10:21:34 · 136 阅读 · 0 评论 -
Leecode<每日一题>复制带随机指针的链表
Leecode<每日一题>复制带随机指针的链表题目链接思路:先将老结点与新结点一一对应,在处理next和random指针。class Solution {public:Node* copyRandomList(Node* head) { unordered_map<Node*, Node*> mp; Node* p = head; while (p) { Node* nnew = new Node(p->val);原创 2021-07-22 10:14:18 · 86 阅读 · 0 评论 -
Leecode<每日一题>两个链表的第一个公共节点
Leecode<每日一题>两个链表的第一个公共节点题目链接思路:先记录两个链表的长度,然后将链表长度长的那个向后移至和短的链表一样长,再遍历链表比对就行了。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {原创 2021-07-21 09:43:48 · 95 阅读 · 0 评论 -
Leecode<每日一题>数组中最大数对和的最小值
Leecode<每日一题>数组中最大数对和的最小值题目链接思路:排序后,用首位加末尾,第二位加倒数第二位,依次相加,维护 最大值。class Solution {public: int minPairSum(vector<int>& nums) { sort(nums.begin(),nums.end()); int mmax = 0; for(int i=0;i<nums.size()/2;i++)原创 2021-07-20 09:32:50 · 81 阅读 · 0 评论 -
Leecode<每日一题>变位词组
Leecode<每日一题>变位词组题目链接思路:排序然后用哈希表判重,再把相同的丢进同一个vector中class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string, vector<string>> mp; for (auto& x : strs) {原创 2021-07-19 17:21:19 · 104 阅读 · 0 评论 -
Leecode<每日一题>最高频元素的频数
Leecode<每日一题>最高频元素的频数题目链接思路:排序+滑动窗口int maxFrequency(vector<int>& nums, int k) { if (nums.size() == 0) return 0; sort(nums.begin(), nums.end()); int res = 1; for (int i = 0, j = 0; i < nums.size() && j < nums.size();) { if原创 2021-07-19 11:08:45 · 116 阅读 · 0 评论 -
Leecode<每日一题>连续子数组的最大和
Leecode<每日一题>连续子数组的最大和题目链接思路:记录前缀和最小的情况,每次用当前前缀和减去最小前缀和,并记录最大值,即为最终答案。int maxSubArray(vector<int>& nums) { int sum = 0; int mmin = 0; int res = -1e9; for (auto& x : nums) { sum += x; res = max(res, sum -原创 2021-07-17 10:36:37 · 130 阅读 · 0 评论 -
Leecode<每日一题>H 指数
Leecode<每日一题>H 指数题目链接思路:计数数组,然后从低到高枚举每一种情况class Solution {public:int hIndex(vector<int>& citations) { vector<int> count_num(citations.size() + 1); for (int i = 0; i < citations.size(); i++) { if (citations[i] &g原创 2021-07-16 11:19:25 · 94 阅读 · 0 评论 -
Leecode<每日一题>在排序数组中查找数字 I
Leecode<每日一题>在排序数组中查找数字 I题目链接思路:二分查找,分别查找最左边和最右边的targetint search(vector<int>& nums, int target) { if (nums.size() == 0) return 0; int l = 0, r = nums.size() - 1; while (l <= r) { int mid = (l + r) / 2; if (原创 2021-07-16 10:21:01 · 94 阅读 · 0 评论 -
Leecode<每日一题>减小和重新排列数组后的最大元素
Leecode<每日一题>减小和重新排列数组后的最大元素题目链接思路:先排序,然后统计符合条件的数的个数,即为最大值class Solution {public: int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) { sort(arr.begin(), arr.end()); int num = 0; for (int i = 0;i<arr.size();i原创 2021-07-15 20:11:20 · 116 阅读 · 0 评论 -
Leecode<每日一题>下一个排列
Leecode<每日一题>下一个排列题目链接思路:从右向左找到第一个非递减的数,然后将其与它右边所有数中比它大一号的数交换,然后将它右边所有剩下的数逆置即可。class Solution {public: void nextPermutation(vector<int>& nums) { int len = nums.size(); if (len == 1) return; bool flag = false; for (int i =原创 2021-07-15 15:45:05 · 152 阅读 · 0 评论 -
Leecode<每日一题>基于时间的键值存储
Leecode<每日一题>基于时间的键值存储题目链接思路:哈希表+二分查找struct node{ string value; int time; node(string v,int t):value(v),time(t) {}};class TimeMap {public: unordered_map<string, vector<node>> mp; /** Initialize your data struc原创 2021-07-10 14:52:23 · 107 阅读 · 0 评论 -
Leecode<每日一题>火柴拼正方形
Leecode<每日一题>火柴拼正方形题目链接思路:预处理的时候判断一下火柴的长度总和是否是4的倍速,然后深度优先搜索,一条边一条边的凑到sum/4,凑到三条边的时候就行了。注意:将火柴的长度从大到小排序,这样false的数据会更快执行完。class Solution {public:bool dfs(vector<int>& matchsticks,int he,int edge,int target){ if (he == target) { edge++;原创 2021-07-09 18:31:41 · 213 阅读 · 0 评论 -
Leecode<每日一题>主要元素
Leecode<每日一题>主要元素题目链接思路:玩一个诸侯争霸的游戏,假设你方人口超过总人口一半以上,并且能保证每个人口出去干仗都能一对一同归于尽。最后还有人活下来的国家就是胜利。那就大混战呗,最差所有人都联合起来对付你(对应你每次选择作为计数器的数都是众数),或者其他国家也会相互攻击(会选择其他数作为计数器的数),但是只要你们不要内斗,最后肯定你赢。最后能剩下的必定是自己人。//转自知乎class Solution {public: int majorityElement(vector&原创 2021-07-09 11:32:33 · 91 阅读 · 0 评论 -
Leecode<每日一题>统计好数字的数目
Leecode<每日一题>统计好数字的数目题目链接思路:其实就是求20的n/2次方,快速幂。#define m (int)(1e9+7)class Solution {public:long long binpow(long long a, long long b) { a %= m; long long res = 1 % m; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >原创 2021-07-08 17:39:58 · 112 阅读 · 0 评论 -
Leecode<每日一题>和相同的二元子数组
Leecode<每日一题>和相同的二元子数组题目链接解题思路:计数数组加前缀和,枚举子数组的结束坐标,并一边记录前缀和与前缀和的计数情况,那么以当前元素结束的连续子数组的个数就为前缀和减去目标数的计数情况,依此遍历一遍即可,官方题解使用哈希表来计数,但在能用数组的情况下,还是优先使用数组,时间效率更高。class Solution {public:static const int mmax = 30010;int mp[mmax] = { 0 };int numSubarraysWithSum原创 2021-07-08 14:35:07 · 137 阅读 · 0 评论 -
Leecode<每日一题>最深叶节点的最近公共祖先
Leecode<每日一题>最深叶节点的最近公共祖先题目链接思路:分三步,先求树的深度,再求最深叶子结点的个数,最后求 公共祖先,都是用深搜,时间复杂度o(n),n为结点个数。class Solution {public: //求最近公共祖先 bool flag = false; TreeNode* res; int dfs(TreeNode* node, int cur, int deep,int leafnum) { if (flag) return leafnum;原创 2021-07-07 17:31:48 · 155 阅读 · 0 评论 -
Leecode<每日一题>二叉树的最近公共祖先
Leecode<每日一题>二叉树的最近公共祖先题目链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/思路:深度优先搜索,当到达每个结点时,计算左子树、右子树和自身中包含两个目标节点的个数,要注意的是,当第一次找到个数为2的结点时,其实就是我们要找的答案。class Solution {public: bool flag = false; TreeNode* res; int dfs(Tree原创 2021-07-07 16:19:13 · 115 阅读 · 0 评论 -
Leecode<每日一题>大餐计数
Leecode<每日一题>大餐计数题目链接:https://leetcode-cn.com/problems/count-good-meals/解题思路:先将数组中的元素计数,再遍历每一个元素,对每个元素枚举是否有另外一个元素与其相加等于2的幂,最后注意将结果除以2,效果还不错。class Solution {public:int cot[1100000] = {0};int countPairs(vector<int>& deliciousness) { for原创 2021-07-07 10:32:49 · 226 阅读 · 0 评论 -
Leecode<每日一题>最大数
Leecode<每日一题>最大数题目链接:https://leetcode-cn.com/problems/largest-number/思路:sort快排,自定义排序方法,将两个int型合成l两个long long型再进行比较static bool cmp(int& a, int& b){ int m = a; int n = b; long long fir = a, sec = b; do { fir *= 10;原创 2021-07-06 11:01:28 · 140 阅读 · 0 评论 -
Leecode<每日一题>在排序数组中查找元素的第一个和最后一个位置
Leecode<每日一题>在排序数组中查找元素的第一个和最后一个位置题目链接:https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/解题思路:二分查找目标数,然后再向前向后搜索临界点,算法复杂度其实还是o(n),其实写完代码才想到可以直接二分查找目标数的最左位置和最右位置。class Solution {public: int halfind(vector<int&原创 2021-07-05 18:42:43 · 106 阅读 · 0 评论 -
Leecode<每日一题>错误的集合
Leecode<每日一题>错误的集合题目链接:https://leetcode-cn.com/problems/set-mismatch/思路:遍历标记即可bool check[10010] = { false };vector<int> findErrorNums(vector<int>& nums) { vector<int> rt; for (int i = 0; i < nums.size(); i++) {原创 2021-07-05 16:20:26 · 159 阅读 · 0 评论 -
Leecode<每日一题>原子的数量
Leecode<每日一题>原子的数量题目链接:https://leetcode-cn.com/problems/number-of-atoms/以下是官方题解:此题解在栈中存放哈希表的方法让人眼前一亮,留作纪念。string countOfAtoms(string formula) { int i = 0, n = formula.length(); auto parseAtom = [&]() -> string { string atom;原创 2021-07-05 16:03:04 · 158 阅读 · 0 评论