【问题标题】:Write a program that tests whether there are two 1 lying on the same row or the same column in 2d array编写一个程序,测试二维数组的同一行或同一列是否有两个 1
【发布时间】:2020-03-11 16:25:11
【问题描述】:

我需要编写一个程序,循环遍历一个二维数组,其中元素由 1 或 0 组成,并检查一行或一列中是否有两个 1,如果找到两个 1,则打印 true在同一列或同一行上,我可以停在那里(我不需要数 1)。

所以我计划为行中的 1 创建一个计数器,为列中的 1 创建一个计数器,如果该计数器高于 1,则循环中断并打印。但是计数器不会每行或每列重置,所以目前如果它找到任何两个 1,无论它们的位置如何,都会打印出来。

我尝试在 for 循环的末尾添加一个 rowTotal = 0 & colTotal = 0,但这样做根本找不到任何 1。

另外,这是针对我的数据结构和算法类的,所以我需要提供一个完整的算法,所以我不想为此使用任何函数。任何有关改进我的代码或解决此问题的更好方法的提示将不胜感激。我可以在 Python 或 Java 中做到这一点。

非常感谢

int[][] board = new int[4][4];



// number to look for
        int findNum = 1;
        // initial total 
        int total = 0;  
        // flag variable to end loop
        boolean found = false;       
        // loops only if found is not 
        for (int i = 0; i < board.length && !found; i++)
        {     
            // resets for each new iteration 
            total = 0;  
            // loops only if found is not 
            for(int j = 0; j < board[i].length && !found; j++)
            {              
                //check row
                if(board[i][j] == findNum) {
                    total++;
                }
                // check column
                if(board[j][i] == findNum) {
                    total++;
                }
                // if more total greater than 1 then end
                if(total > 1) {
                    found = true;
                }
            }

        }

【问题讨论】:

  • 在上面发布了更正的代码。因为这是针对 DSA 课程的,所以我无法使用任何其他功能,老师希望它在两个循环中使用。我只需要添加 if(board[j][i] == findNum) {total++;} 感谢他们帮助我清理代码并找到答案的所有答案。尽管如此,我还是花了几个小时才找到这个。

标签: java algorithm loops for-loop multidimensional-array


【解决方案1】:

如果条件更改为使用colTotal 而不是rowTotal,则内部循环更改

 if (colTotal > 1) {
   System.out.println("2 lying on col");
   break;
 } 

请记住,break 不会破坏外部循环,因此您需要一个标志,例如。

 boolean found = false; // outside loops

当您打印和中断时,只需分配 true

 found = true; // before break inside inner loop, adjecent to print statement

现在使用这个标志来检查外部循环内的第一条语句

 if (found) {
    break;
 }

或者只是你可以把它作为外循环的条件

 for (int i = 0; i < board.length && !found; i++)

【讨论】:

  • 感谢您的洞察力,我完全忘记了我的 break 不会破坏外循环。如何将所有内容放在一行中也非常有帮助 - for (int i = 0; i
【解决方案2】:

创建两组:一组用于行,另一组用于列。

遍历数组。

每次您找到一个带有 set 位的单元格时,请检查该单元格在您的集合中的行和列。如果其中任何一个都存在,那么您就完成了。如果没有,请更新集合并继续。

【讨论】:

    【解决方案3】:

    我认为您可以逐行遍历整个二维数组并检查是否有多个 1,现在转置数组并重复该过程以逐列检查:

     public static void main(String[] args) {
    
        int[][] board = {   { 0, 0, 0, 0 }, 
                            { 0, 1, 0, 0 }, 
                            { 1, 0, 0, 1 }, 
                            { 0, 0, 1, 0 } };
    
        if (checkMoreThanOne(board)) {
            System.out.println("Found more than one 1's in rows.");
        }
        int[][] transpose = transpose(board);
        if (checkMoreThanOne(transpose)) {
            System.out.println("Found more than one 1's in columns.");
        }
    }
    
    private static boolean checkMoreThanOne(int[][] board) {
        for (int i = 0; i < board.length; i++) {
            int count = 0;
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 1) {
                    if (count > 0)
                        return true;
                    else
                        count++;
                }
            }
        }
        return false;
    }
    
    private static int[][] transpose(int[][] array) {
        if (array == null || array.length == 0)
            return array;
    
        int width = array.length;
        int height = array[0].length;
    
        int[][] array_new = new int[height][width];
    
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                array_new[y][x] = array[x][y];
            }
        }
        return array_new;
    }
    

    【讨论】:

    • 可以替换 System.exit(0);带有适当的返回语句等。
    • 太棒了,非常感谢。但是,这只会检查下一个元素是否为 1,而我只需要检查它们是否在同一行上。所以如果第一行是 1001 那么我应该打印 true。反正有没有改变行: if(j
    • 抱歉问题我之前没有搞清楚,我现在修改了答案。
    猜你喜欢
    • 2021-07-09
    • 2020-06-08
    • 2011-02-04
    • 1970-01-01
    • 2021-12-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多