class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
// 暴力会超时,因为时间复杂度是o(n^2)
// 动态规划的思想
int n = nums.length;
int[] dp = new int[n]; // 初始化为0
dp[0] = 1;
// 先计算dp数组
for (int i = 1; i < n; i++){
if (((nums[i] ^ nums[i-1]) & 1) == 1){
// 奇偶性不同
dp[i] = dp[i-1] + 1;
}
else dp[i] = 1;
}
boolean[] ans = new boolean[queries.length];
// 遍历queries
for (int i = 0; i < queries.length; i++){
int m = queries[i][1] - queries[i][0] + 1; // 计算区间长度
if (dp[queries[i][1]] >= m){
// 返回true
ans[i] = true;
}
else ans[i] = false;
}
return ans;
}
}
3152.特殊数组||
最新推荐文章于 2024-09-03 18:29:17 发布