6889. 特殊元素平方和 - 力扣(LeetCode)
思路:模拟
class Solution { public: int sumOfSquares(vector<int>& nums) { int res = 0; for(int i = 0; i < nums.size(); i ++ ) { if(nums.size() % (i + 1) == 0) res += nums[i] * nums[i]; } return res; } };
6929. 数组的最大美丽值 - 力扣(LeetCode)
思路:排序+双指针
class Solution { public: int maximumBeauty(vector<int>& nums, int k) { int n = nums.size(); int res = 0, left = 0; sort(nums.begin(), nums.end()); for(int right = 0; right < n; right ++ ) { while (nums[right] - nums[left] > k * 2) left ++ ; res = max(res, right - left + 1); } return res; } };
6927. 合法分割的最小下标 - 力扣(LeetCode)
思路:哈希+枚举
class Solution { public: int minimumIndex(vector<int>& nums) { map<int, int> mp; int k = 0, cnt = 0; for(int t : nums) { mp[t] ++ ; if(mp[t] > cnt) { cnt = mp[t]; k = t; } } int n = nums.size(); int count = 0; for(int i = 0; i < n; i ++ ) { if(nums[i] == k) count ++ ; if(count > (i + 1) / 2 && (cnt - count) > (n - 1 - i) / 2) return i; } return -1; } };
6924. 最长合法子字符串的长度 - 力扣(LeetCode)文章来源:https://www.toymoban.com/news/detail-599039.html
思路:哈希+双指针文章来源地址https://www.toymoban.com/news/detail-599039.html
class Solution { public: int longestValidSubstring(string word, vector<string>& forbidden) { unordered_set<string> se{forbidden.begin(), forbidden.end()}; int res = 0, left = 0, n = word.size(); for(int right = 0; right < n; right ++ ) { for(int i = right; i >= left && i > right - 10; i -- ) { if(se.count(word.substr(i, right - i + 1))) { left = i + 1; break; } } res = max(res, right - left + 1); } return res; } };
到了这里,关于leetcode第354场周赛补题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!