我想知道一个整数数组是否是交替的。在 JAVA 中。
例如:
a[]={1,-1,1,-1,1,-1} --> true
a[]={-1,1,-1,1,-1} --> true
a[]={1,-4,1-6,1} --> true
a[]={1,1,1,14,5,3,2} --> false
我已经开始编写一些使用标志的代码。例如,如果current_is_positive=0
and else = 1
,但我没有得到任何地方。有什么好的方法可以达到这种效果?
我想知道一个整数数组是否是交替的。在 JAVA 中。
例如:
a[]={1,-1,1,-1,1,-1} --> true
a[]={-1,1,-1,1,-1} --> true
a[]={1,-4,1-6,1} --> true
a[]={1,1,1,14,5,3,2} --> false
我已经开始编写一些使用标志的代码。例如,如果current_is_positive=0
and else = 1
,但我没有得到任何地方。有什么好的方法可以达到这种效果?
我认为您的意思是符号交替,即正数,负数,正数等?您可以使用以下策略:
跳过第一个元素。
对于每个其他元素,将其符号与前一个元素的符号进行比较:
由于这听起来像是一项家庭作业,因此我将由您自己用 Java 编写适当的代码。
这是我的解决方案:
这将检查元素 n+1 是否是元素 n 的倒数。
public static void main(String[] args) {
int[] ints = {1, -1, 2, -1};
System.out.println(new Example().isArrayAlternating(ints));
}
public boolean isArrayAlternating(int[] ints) {
if (ints == null || ints.length % 2 != 0) {
return false;
}
for (int i = 0; i < ints.length - 1; i++) {
if (ints[i] != ints[i + 1]*(-1)) {
return false;
}
}
return true;
}
如果您只想检查正数,负数...n次,而不关注值:
public static void main(String[] args) {
int[] ints = {1, -1, 2, -1};
System.out.println(new Example().isArrayAlternating(ints));
}
public boolean isArrayAlternating(int[] ints) {
if (ints == null || ints.length % 2 != 0) {
return false;
}
for (int i = 0; i < ints.length - 1; i++) {
if (ints[i] >= 0 && ints[i + 1] >= 0 || ints[i] <= 0 && ints[i + 1] <= 0) {
return false;
}
}
return true;
}
private enum SIGN {
POSITIVE, NEGATIVE
};
public static boolean isAlternating(int... ints) {
SIGN last = null;
for (int i : ints) {
if (i >= 0) {
if (last == null) {
last = SIGN.POSITIVE;
} else {
if (last == SIGN.POSITIVE) {
return false;
} else {
last = SIGN.POSITIVE;
}
}
} else {
if (last == null) {
last = SIGN.NEGATIVE;
} else {
if (last == SIGN.NEGATIVE) {
return false;
} else {
last = SIGN.NEGATIVE;
}
}
}
}
return true;
}
您可以简单地检查所有项目是否等于后退两步的项目。我不知道您使用的是什么语言,但例如使用 C#,您可以这样做:
bool alternating = a.Skip(2).Where((n, i) => a[i] == n).Count() == a.Length - 2;
所以你想检查值的符号是否是交替的,而不是值?
然后只需对照上一项检查标志:
bool alternating = a.Skip(1).Where((n,i) => Math.Sign(n) == -Math.Sign(a[i])).Count() == a.Length-1;
boolean prevPositive = arr[0] > 0, error = false;
for (int i = 1; i < arr.length; ++i) {
boolean current = arr[i] > 0;
if (current != prevPositive) {
current = prevPositive;
} else {
error = true;
break;
}
}
if (error)
System.out.println("No");
else
System.out.println("Yes");
0, 2, 4, 6, ...
然后从索引循环 -1, 3, 5, 7, ...
然后检查两个循环中的每个数字与该迭代中的第一个数字的乘积是否应该是正数。
int a[]={1,-1,1,-1,1,-1};
boolean alternating = true;
for (int i = 0; i < a.length; i = i + 2) {
if (a[i] * a[0] > 0) {
} else {
alternating = false;
}
}
for (int i = 1; i < a.length; i = i + 2) {
if (a[i] * a[1] > 0) {
} else {
alternating = false;
}
}
if (alternating) {
System.out.println("Array is alternating");
} else
System.out.println("Array is not alternating");
}
从索引 1 遍历数组直到结束。
在每个索引处,评估(a[i] > 0) == (a[i-1] > 0)
。如果这是真的,那么你的数组不是交替的。
如果你坚持到最后却没有断定它不是交替的,那么它就是交替的:)
For i = 2 to n
check whether A[i-1] && A[i] are with diff sign..
in C++; return ((A[i-1] ^ A[i]) < 0).
此处解释相同:http ://www.youtube.com/watch?v=Z59REm2YKX0
编辑
如果一个整数为负数,则高位为 1。否则为 0。您可以通过将两个整数异或在一起来检查它们是否具有不同的符号。如果符号不同,则结果的高位为 1。如果它们相同,则高位为 0。因此,
A XOR B < 0 相当于“A 和 B 有不同的符号”
彼得·鲁德曼