摘要:1191. K 次串联后最大子数组之和 很蠢但能通过的方法:k >= 3时,算三次res。如果res2 - res2 == res3 - res2,说明为等差数列。如果res2 - res2 != res3 - res2,说明循环多次的结果都是一样的。 class Solution { public
阅读全文
摘要:1749. 任意子数组和的绝对值的最大值 没做出来🤡 法一:动态规划 class Solution { public: int maxAbsoluteSum(vector<int>& nums) { int res = 0; // 初始化以当前元素为结尾的子数组的最大和f_max为0 int f_
阅读全文
摘要:2606. 找到最大开销的子字符串 class Solution { public: int maximumCostSubstring(string s, string chars, vector<int>& vals) { int size = s.size(); vector<int> dp(s
阅读全文
摘要:213. 打家劫舍 II 与 198. 打家劫舍 相比,多了首和尾不能同时偷的条件 但是没写出来🤡 看了题解,可以比较 不偷首 和 不偷尾 ,哪个大就是最终答案 class Solution { public: int rob2(vector<int>& nums,int left,int rig
阅读全文
摘要:3186. 施咒的最大总伤害 这道题相比 740. 删除并获得点数 ,区别是这道题的元素值可以特别大,所以就不能开大数组。 没做出来🤡 法一:记忆化搜索 class Solution { public: //定义 dfs(i) 表示从 a[0] 到 a[i] 中选择,可以得到的伤害值之和的最大值。
阅读全文
摘要:2320. 统计放置房子的方式数 class Solution { public: const int MOD = 1'000'000'007; int countHousePlacements(int n) { if(n == 1) return 4; vector<long> dpZero(n+
阅读全文
摘要:740. 删除并获得点数 凭着感觉,莫名其妙迷迷糊糊地就通过了,自己也不知道怎么就过了,以下是题解的评论 // 每个位置上的数字是可以在两种前结果之上进行选择的: // 1.如果你不删除当前位置的数字,那么你得到就是前一个数字的位置的最优结果。 // 2.如果你觉得当前的位置数字i需要被删,那么你就
阅读全文
摘要:2266. 统计打字方案数 题目挺简单的,就是溢出、取余特别令人抓狂 class Solution { public: const int MOD = 1'000'000'007; int count(const int &choices,const int &num){ if(num <= 2)
阅读全文
摘要:2466. 统计构造好字符串的方案数 没写出来🤡 题解 法一:记忆化搜索 class Solution { public: // 计算在[low, high]范围内,满足'0'的数量不超过zero,'1'的数量不超过one的字符串数量 int countGoodStrings(int low, i
阅读全文
摘要:377. 组合总和 Ⅳ 没写出来🤡 官方题解 class Solution { public: //当1 ≤ i ≤ target 时,如果存在一种排列,其中的元素之和等于 i,则该排列的最后一个元素一定是数组 nums 中的一个元素。 //假设该排列的最后一个元素是 num,则一定有 num ≤
阅读全文
摘要:746. 使用最小花费爬楼梯 class Solution { public: int minCostClimbingStairs(vector<int>& cost) { int n = cost.size(); vector<int> dp(n+1);//dp[i]代表到达第 i 层的最小花费
阅读全文
摘要:475. 供暖器 没做出来🤡 法一:排序 + 二分查找 class Solution { public: //lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。 //在从小到大的排序数组中,lower_bound(begin,end,n
阅读全文
摘要:826. 安排工作以达到最大收益 首先是自己写的构思代码 class Solution { public: int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker){ sort
阅读全文
摘要:870. 优势洗牌 没做出来🤡 题解 class Solution { public: //类比田忌赛马,数组中最小值即下等马,最大值上等马。每次用nums1中的下等马去跟nums2中的下等马pk //如果干得过就干,干不过就用nums1中的下等马去当炮灰,去干nums2中的上等马 //在本题中,
阅读全文
摘要:2576. 求出最多标记下标 题解: 这道题从左往右扫描和从右往左扫描都是错的。 比如 : 1 1 1 1 2 2 2 4 ,从右往左,如果将倒数的2 和 4 匹配了,那么剩下的都不能匹配了。但实际上全部都能匹配。 比如: 1 2 2 2 5 5 5 5,从左往右,如果将 1 和 2 匹配,两个 2
阅读全文
摘要:2592. 最大化数组的伟大值 法一:排序 丑陋的代码 class Solution { public: int maximizeGreatness(vector<int>& nums) { sort(nums.begin(),nums.end()); int size = nums.size(),
阅读全文
摘要:8. 字符串转换整数 (atoi) 丑陋的代码 class Solution { public: int myAtoi(string s) { int i = 0,size = s.size(); bool isPositive = true; long res = 0; while(s[i] ==
阅读全文
摘要:1481. 不同整数的最少数目 法一: class Solution { public: int findLeastNumOfUniqueInts(vector<int>& arr, int k) { unordered_map<int,int> numAdded; for(int &num : a
阅读全文
摘要:866. 回文质数 想着开大数组,用质数筛选的方法。但是开大数组超内存了🤡 class Solution { public: bool isPrime[200000001]; bool isPalind(int &num){ string str = to_string(num); int i =
阅读全文
摘要:228. 汇总区间 class Solution { public: vector<string> summaryRanges(vector<int>& nums) { int size = nums.size(); if(size == 1) return {to_string(nums[0])}
阅读全文
摘要:658. 找到 K 个最接近的元素 法一: class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, int x) { vector<pair<int,int>> dist; vector<in
阅读全文
摘要:977. 有序数组的平方 法一:双指针,先找负数与非负数的边界 class Solution { public: vector<int> sortedSquares(vector<int>& nums) { vector<int> res; if(nums[0] >= 0){//全部非负的情况 fo
阅读全文
摘要:1750. 删除字符串两端相同字符后的最短长度 注意审题,是相同的字符,而不是相同的字符串。所以对于 abcccab 来说就是输出7 class Solution { public: int minimumLength(string s) { int left = 0,right = s.size(
阅读全文
摘要:125. 验证回文串 二刷,用时3ms,内存9.81MB 一定要注意,是移除所有除了数字、字母以外的字符 class Solution { public: //'a' - 'A' = 32 bool isPalindrome(string s) { int left = 0,right = s.si
阅读全文
摘要:1838. 最高频元素的频数 直接看题解 法一:滑动窗口 class Solution { public: int maxFrequency(vector<int>& nums, int k) { // 首先对数组进行排序,这是为了后续计算元素之间的差值时能够简化计算 sort(nums.begin
阅读全文
摘要:2516. 每种字符至少取 K 个 逆向思维:滑动窗口内的字符a最多个数为(原字符串a的个数 - k),b和c同理。求出这个滑动窗口最长长度res,结果返回size-res class Solution { public: int takeCharacters(string s, int k) {
阅读全文
摘要:质数判断 六步进法判断素数的原理是基于素数的分布规律。对于大于等于5的自然数,素数一定出现在6的倍数的两侧,即6x和6x+1的位置上。如11和13,17和19。大于等于5的质数一定和6的倍数相邻,具体表现为6x-1, 6x, 6x+1, 6x+2, 6x+3, 6x+4, 6x+5, 6(x+1
阅读全文
摘要:1658. 将 x 减到 0 的最小操作数 其实就是个不定长的滑动窗口 暴力超时解🤡 class Solution { public: int sum(int left,int right,vector<int>& nums,int size){ int res = 0; if(left <= r
阅读全文
摘要:2024. 考试的最大困扰度 法一:两次滑动窗口 class Solution { public: int maxConsecutiveAnswers(string answerKey, int k) { int size = answerKey.size(),temp = k,res = 0,le
阅读全文
摘要:2779. 数组的最大美丽值 暴力超时解🤡 class Solution { public: int maximumBeauty(vector<int>& nums, int k) { int size = nums.size(),res = 0; unordered_map<int,int> n
阅读全文
摘要:2958. 最多 K 个重复元素的最长子数组 class Solution { public: int maxSubarrayLength(vector<int>& nums, int k) { int size = nums.size(),resLenth = 0; unordered_map<i
阅读全文
摘要:258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。其中 0 ≤ num ≤ 2^31-1 法一:迭代 class Solution { public: int addDigits(int num) { while(num >= 10){//判断千
阅读全文
摘要:1695. 删除子数组的最大得分 法一:使用set 或者 unordered_set class Solution { public: int maximumUniqueSubarray(vector<int>& nums) { int size = nums.size(),resMax = 0,n
阅读全文
摘要:904. 水果成篮 说白了就是:找最多包含两种元素的最长子串,返回其长度 值得注意的是,当窗口内有三种种类时,左窗口边界是要向右移动到窗口内只剩两种种类,而不是什么先进先出!比如 [1,0,1,4,1,4,1,2,3] 法一:unordered_map class Solution { public
阅读全文
摘要:两道题差不多思路,放到一起 1208. 尽可能使字符串相等 其中,字符串 s 和 t 只包含小写字母 法一:使用额外空间 class Solution { public: int equalSubstring(string s, string t, int maxCost) { int size =
阅读全文
摘要:1493. 删掉一个元素以后全为 1 的最长子数组 法一:递推 class Solution { public: //在删掉元素的结果数组中,最长的且只包含 1 的非空子数组存在两种情况: //1.这个子数组在原数组中本身就是连续的,无论删或者不删其他的元素,它都是最长的且只包含 1 的非空子数组;
阅读全文
摘要:3090. 每个字符最多出现两次的最长子字符串 给你一个字符串 s ,请找出满足每个字符最多出现两次的最长子字符串,并返回该子字符串的最大长度。 使用unordered_map class Solution { public: int maximumLengthSubstring(string s)
阅读全文
摘要:3. 无重复字符的最长子串 给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。 滑动窗口模板 //外层循环扩展右边界,内层循环扩展左边界 for (int l = 0, r = 0 ; r < n ; r++) { //当前考虑的元素 while (l <= r && check()
阅读全文
摘要:2653. 滑动子数组的美丽值 暴力超时解,纯批判性展示🤡 class Solution { public: vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x) { int size = nums.size(); if(si
阅读全文
摘要:1297. 子串的最大出现次数 暴力解,超时🤡 class Solution { public: int maxFreq(string s, int maxLetters, int minSize, int maxSize) { int size = s.size(), maxRes = 0; u
阅读全文
摘要:1652. 拆炸弹 原本是简单题,但是k < 0的情况由于选用的方法不好,浪费太多时间了。代码也有很多冗余 class Solution { public: vector<int> decrypt(vector<int>& code, int k) { int size = code.size();
阅读全文
摘要:1423. 可获得的最大点数 首先,前 k 个数和后 k 个数 的 较大者并不是正确答案,比如 100 40 17 9 73 75,正确解是248。 其次,想到了前或者后拿了一个数之后,就是求剩下序列拿k-1个数,可以转换成子问题,所以想到了递归。但是k比较大的时候就超时了: class Solut
阅读全文
摘要:两道题本质是一样的,所以放一起了 2841. 几乎唯一子数组的最大和 使用unordered_map;unordered_multiset可能也可以,但是不如前者方便 class Solution { public: long long maxSum(vector<int>& nums, int m
阅读全文
摘要:1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 使用unordered_set ,通过集合数量来判断 法一:将二进制数转化为十进制数,放到集合中。此法用时139ms,内存48.2MB class Solution { public: bool hasAllCodes(string s
阅读全文
摘要:1456. 定长子串中元音的最大数目 法一:借助队列 class Solution { public: int maxVowels(string s, int k) { int size = s.size(), resMax = 0; queue<bool> qVowel; for(int i =
阅读全文