有人能帮我吗?我正在计算某些数字在表格布局行中出现的次数——换句话说,是垂直和水平。我想填充一个介于(4和5)之间的数字数组,这样数字(4)在6*6中只出现4次,5只出现两次(垂直和水平)。注意
我如何处理这些问题
public boolean hasRepeatedNumbers(int[] x) {
int[] y = new int[x.length];
System.arraycopy(x, 0, y, 0, y.length);
Array.sort(y);
int i;
for (i = 1; i < y.length; i++) {
if (y[i] == y[i-1]) return true;
}
return false;
}
或
private int[] calculateUsedCells(int x, int y) {
int c[] = new int[2];
// horizontal
for (int i = 0; i < 2; i++) {
if (i == y)
continue;
int t = getCell(x, i);
if (t != 0)
c[t - 1] = t;
}
}
任何建议都很好,谢谢。
考虑取一个int数组,并在单元格中值的索引处增加数组中的元素,最后检查数组中的值。您将得到每个数字出现的次数。
例如:
编号4出现在1、2、5、6个细胞中
array[content of the cell]++;
因此,在末尾数组[4]给出了4出现的次数。