Leetcode 3254. 长度为 K 的子数组的能量值 I
 1 class Solution {
 2 public:
 3     vector<int> resultsArray(vector<int>& nums, int k) {
 4         int n = nums.size();
 5         int cnt = 0;
 6         vector<int> ans(n - k + 1, -1);
 7         for (int i = 0; i <= n - k; i++) {
 8             bool valid = true;
 9             for (int j = i + 1; j < i + k; j++) {
10                 if (nums[j] - nums[j - 1] != 1) {
11                     valid = false;
12                     break;
13                 }
14             }
15             if (valid) {
16                 ans[i] = nums[i + k - 1];
17             }
18         }
19         return ans;
20     }
21 };

 

posted on 2024-11-06 21:21  greenofyu  阅读(5)  评论(0编辑  收藏  举报