【问题标题】:How to find duplicate in a matrix/array while ignoring one specific value?如何在忽略一个特定值的同时在矩阵/数组中查找重复项?
【发布时间】:2023-03-21 00:46:02
【问题描述】:

我目前正在用 java 编写一个数独游戏,一个规则是每一行不能包含多次相同的数字 (1-9)。数字 0 表示该框为空。 如果找到重复项,我的“checkRow”布尔方法应该返回“false”,如果没有找到重复项(正确的行),则返回“true”。我知道如何查找重复项,但由于值 '0' 表示一个空框,因此允许重复项 '0'。

我该如何做到这一点?

这是迄今为止我想出的代码。每个测试用例都返回“true”(未找到重复项)。

public boolean checkRow(int row){
    
       for (int k = 0; k < grid.length; k++){
            for (int i = k + 1; i < grid.length; i++){
                if (grid[row][k] == (grid[row][i]) && grid[row][k] != 0) {
                    return false;                       
        }
    }
}
return true;
}

使用加法数组存储找到的值的代码:

public boolean checkRow(int row){
  
  int[] found = new int[9];
  for (int k = 0; k < grid.length; k++){
      if (grid[row][k] > 0 && grid[row][k] < 10){
          found[grid[row][k]]++;
      }
 
      if (found[grid[row][k]] > 1){
          return false;
      }
      
  
     }
    return true;                
}

这也只返回 true。

提前致谢!

【问题讨论】:

  • 您的代码看起来正确,有什么问题?
  • 我可能不会使用嵌套循环,而是使用一个临时数组来存储每个数字的出现次数(该数字将是数组的索引),如果该数字 > 1 用于其他任何索引比 0 你发现了一个重复。这样就只需要一个循环。
  • @Thomas,我现在已经想出了该解决方案的代码。但是,它每次都返回 true。用新代码更新了帖子,cmets中的代码很难阅读。
  • 您是否调试过代码以查看grid 是否是正确的数组、是否具有正确的长度等?

标签: java arrays matrix duplicates


【解决方案1】:

根据@Thomas 的评论,解决方案可能如下(不使用全局grid 变量)

public static boolean isRowValid(int[] row) {
    int[] freq = new int[9];

    for (int num : row) {
        if (num > 0 && freq[num - 1]++ > 0) {
            // debugging print
            System.out.printf("Duplicate %d found%n", num);
            return false;
        }
    }
    return true;
}

测试

int[][] grid = {
    {1, 2, 3, 4, 5, 6, 7, 8, 9},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 2, 0, 0, 8, 0},
    {0, 1, 0, 0, 2, 0, 0, 1, 0},
    {1, 2, 3, 4, 5, 6, 7, 8, 1},
    {1, 2, 3, 4, 1, 6, 7, 8, 9},
    {0, 2, 0, 3, 1, 4, 0, 1, 1},
    {3, 2, 0, 1, 3, 0, 3, 0, 0},
    {7, 2, 0, 1, 3, 0, 3, 0, 7},
};

for (int[] row : grid) {
    System.out.printf("Row %s has no dups? %s%n", Arrays.toString(row), isRowValid(row));
}

输出

Row [1, 2, 3, 4, 5, 6, 7, 8, 9] has no dups? true
Row [0, 0, 0, 0, 0, 0, 0, 0, 0] has no dups? true
Row [0, 1, 0, 0, 2, 0, 0, 8, 0] has no dups? true
Duplicate 1 found
Row [0, 1, 0, 0, 2, 0, 0, 1, 0] has no dups? false
Duplicate 1 found
Row [1, 2, 3, 4, 5, 6, 7, 8, 1] has no dups? false
Duplicate 1 found
Row [1, 2, 3, 4, 1, 6, 7, 8, 9] has no dups? false
Duplicate 1 found
Row [0, 2, 0, 3, 1, 4, 0, 1, 1] has no dups? false
Duplicate 3 found
Row [3, 2, 0, 1, 3, 0, 3, 0, 0] has no dups? false
Duplicate 3 found
Row [7, 2, 0, 1, 3, 0, 3, 0, 7] has no dups? false

使用流 API 的等效解决方案如下所示:

public static boolean isRowValidStream(int[] row) {
    int[] freq = new int[9];
    return Arrays.stream(row) // getIntStream
                 .filter(x -> x > 0) // filter out all zeroes
                 .map(x -> freq[x - 1]++) // calculate frequency
                 .noneMatch(f -> f > 0); // look for the first frequency value 1 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多