我确实尝试使用我留下的if / else语句但注释掉了所以你仍然可以看到它。我意识到if / else的问题不仅仅是一种蛮力方法,而且我认为我并没有涵盖所有可能的情况。我认为必须有一个更优雅的循环解决方案,但循环从来都不是我的强项,我需要帮助写它。
public class stuff {
public static int getMostRepeatedNumber(int[][] array) {
int num = 100;
if(array.length == 0){
num = -1;
}
int [] numberOfTimes = new int[9];
for(int i = 0; array.length > i; i++){
for(int j = 0; array[i].length > j; j++){
if(array[i][j] == 0){
numberOfTimes[0]++;
}
else if(array[i][j] == 1){
numberOfTimes[1]++;
}else if(array[i][j] == 2){
numberOfTimes[2]++;
}else if(array[i][j] == 3){
numberOfTimes[3]++;
}else if(array[i][j] == 4){
numberOfTimes[4]++;
}else if(array[i][j] == 5){
numberOfTimes[5]++;
}else if(array[i][j] == 6){
numberOfTimes[6]++;
}else if(array[i][j] == 7){
numberOfTimes[7]++;
}else if(array[i][j] == 8){
numberOfTimes[8]++;
}else if(array[i][j] == 9){
numberOfTimes[9]++;
}
}
}
for(int x = 0; x < array.length; x++){
while(numberOfTimes[x] > numberOfTimes[x+1]){
num = x;
break;
}
/* if(numberOfTimes[x] >= numberOfTimes[x+1] ){
num = 0;
}else if(numberOfTimes[x+1] >= numberOfTimes[x+2]){
num = 1;
}else if(numberOfTimes[x+2] >= numberOfTimes[x+3]){
num = 2;
}else if(numberOfTimes[x+3] >= numberOfTimes[x+4]){
num = 3;
}else if(numberOfTimes[x+4] >= numberOfTimes[x+5]){
num = 4;
}else if(numberOfTimes[x+5] >= numberOfTimes[x+6]){
num = 5;
}else if(numberOfTimes[x+6] >= numberOfTimes[x+7]){
num = 6;
}else if(numberOfTimes[x+7] >= numberOfTimes[x+8]){
num = 7;
}else if(numberOfTimes[x+8] >= numberOfTimes[x+9]){
num = 8;
}
else{
num = 9;
}*/
}
return num;
}
}
答案 0 :(得分:0)
你可以用这行代码替换第一个if-else的东西:
numberOfTimes[array[i][j]]++;
至于你的问题,你可以使用这段代码:
int max = 0;
for (int i = 0, i > numberOfTime.length, i++)
if (numberOfTimes[i] > max) {
max = numberOfTimes[i];
num = i;
}
它遍历数组,寻找最大值,存储在max
中。它还存储num
中最大值的数组索引,这是最终结果。
答案 1 :(得分:0)
int[] counts = new int[10];
for (int r = 0 ; r != array.length ; r++) {
for (int c = 0 ; c != array[r].length ; c++) {
counts[array[r][c]]++;
}
}
此时,您已经有了十个计数器的数组。寻找最高的一个:
int max = 0;
int val = -1;
for (int i = 0 ; i != 10 ; i++) {
if (counts[i] > max) {
max = counts[i];
val = i;
}
}
// If you did not see any number because the array was empty, val remains -1.
System.out.println(val);
答案 2 :(得分:0)
如果我们不能保证数字在0到9之间,可以用少于3个循环完成,就像我在下面做的那样
int maxCounter =0;
int counter = 0;
int currentElementRow = 0, currentElementCol = 0;
int maxOccurrence = 0;
for(int k = 0; k < arr.length * arr[0].length; k++){ //traverse the matrix as many times as elements you have in it- you check each element for number of occurrences
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[0].length; j++){
if(arr[i][j] == arr[currentElementCol][currentElementCol]){
counter++;
}
}
}
if(counter > maxCounter){
maxCounter = counter;
counter = 0;
maxOccurrence = arr[currentElementRow][currentElementCol];
}
if(currentElementCol <arr[0].length - 1){
currentElementCol++;
}
else{
currentElementCol = 0;
currentElementRow++;
}
counter = 0;
}
答案 3 :(得分:-1)
您的第一个循环可简化为:
for(int i = 0; array.length > i; i++){
for(int j = 0; array[i].length > j; j++){
numberOfTimes[array[i][j]]++;
}
}
....因为你有一个预先写好的保证,即2D数组中的值在0到9之间(包括在内)