前言:想了好一会才想到是前缀和来写,并且我一开始的是从考虑这个数和这个数后面一个数,导致边界烦了我好久
看了一下,考虑这个数和这个数前一个数更好
class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
vector<int> s(nums.size());
for (int i = 1; i < nums.size(); i++) {
s[i] = s[i - 1] + (nums[i - 1] % 2 == nums[i] % 2);
}
vector<bool> ans(queries.size());
for (int i = 0; i < queries.size(); i++) {
auto& q = queries[i];
ans[i] = s[q[0]] == s[q[1]];
}
return ans;
}
};