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. 數(shù)組的最大美麗值 - 力扣(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. 合法分割的最小下標(biāo) - 力扣(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. 最長(zhǎng)合法子字符串的長(zhǎng)度 - 力扣(LeetCode)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-599039.html
思路:哈希+雙指針文章來(lái)源地址http://www.zghlxwxcb.cn/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; } };
到了這里,關(guān)于leetcode第354場(chǎng)周賽補(bǔ)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!