class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
if(nums.size()==1)
return true;
bool flag;
if(nums[0]%2==1)
flag=true;
else
flag=false;
for(int i=1;i<nums.size();i++)
{
if(!flag&&nums[i]%2==1)
{
flag=true;
continue;
}
else if(flag&&nums[i]%2==0)
{
flag=false;
continue;
}
else
return false;
}
return true;
}
};
#include <vector>
using namespace std;
class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
vector<bool> result;
int n = nums.size();
vector<int> paritySwitch(n, 0);
// 预处理奇偶切换点
for (int i = 1; i < n; ++i) {
paritySwitch[i] = paritySwitch[i-1] + ((nums[i-1] % 2) == (nums[i] % 2));
}
// 处理每个查询
for (const auto& query : queries) {
int l = query[0];
int r = query[1];
if (l == r) {
result.push_back(true);
} else {
result.push_back(paritySwitch[r] == paritySwitch[l]);
}
}
return result;
}
};
class Solution {
public:
long long sumDigitDifferences(vector<int>& nums) {
int numDigits = to_string(nums[0]).size();
vector<unordered_map<char, int>> digitCount(numDigits);
// 填充计数
for (int num : nums) {
string numStr = to_string(num);
for (int i = 0; i < numDigits; ++i) {
digitCount[i][numStr[i]]++;
}
}
// 计算总和
long long totalDiffSum = 0;
for (int i = 0; i < numDigits; ++i) {
auto& counts = digitCount[i];
for (auto it1 = counts.begin(); it1 != counts.end(); ++it1) {
for (auto it2 = counts.begin(); it2 != counts.end(); ++it2) {
if (it1->first != it2->first) {
totalDiffSum += static_cast<long long>(it1->second) * it2->second;
}
}
}
}
return totalDiffSum / 2; // 每对计算了两次,所以除以2
}
};