我们来分析一下问题。交替奇偶校验是什么意思?
index : value : value + index
------------------------------
0 : 1 : 1 - note, all sums are odd
1 : 2 : 3
2 : 7 : 9
....
或者
index : value : value + index
------------------------------
0 : 2 : 2 - note, all sums are even
1 : 1 : 2
2 : 6 : 8
....
如您所见(您可以轻松证明)交替奇偶校验意味着
index + value 和是 all odd 或 all even。让我们在 Linq 的帮助下检查一下:
List<int> numbers = new List<int>() { 1, 2, 7, 20, 3, 79 };
bool result = numbers
.DefaultIfEmpty()
.Select((item, index) => Math.Abs((long)item + (long)index))
.Aggregate((s, a) => s % 2 == a % 2 ? s % 2 : -1) >= 0;
实施注意事项:
-
DefaultIfEmpty() - 空序列的所有(全零)值交替出现;但是,Aggregate 没有可聚合的内容并引发异常。让我们把空序列变成一个元素序列。
-
(long) 以防止整数溢出(int.MaxValue + index 很可能超出 int 范围)
-
Math.Abs:c# 可以返回负余数(例如-1 % 2);我们不希望对此进行额外检查,所以让我们使用绝对值
- 但是,我们可以在最终的
Aggregate 中利用此效果(-1 % 2 == -1)
Extension Method 解决方案我希望更容易理解:
public static bool IsAlternating(this IEnumerable<int> source) {
if (null == source)
throw new ArgumentNullException(nameof(source));
bool expectedZero = false;
bool first = true;
foreach (int item in source) {
int actual = item % 2;
if (first) {
first = false;
expectedZero = actual == 0;
}
else if (actual == 0 && !expectedZero || actual != 0 && expectedZero)
return false;
expectedZero = !expectedZero;
}
return true;
}
注意,循环解决方案(扩展方法)更有效:当模式不满足时,它会返回false立即。