写一个有效的方法

时间:2014-01-10 19:52:17

标签: algorithm optimization matrix

我被指示编写一种方法,用于在方阵中找到“交叉”(此问题中的所有矩阵都包含1和0)。

k是交叉如果在第k行中所有元素都是零0并且在第k列中所有元素都是1(除了[k] [k]中等于0的那个) )

例如,2是该矩阵中的叉号:
1 0 1
0 1 1
0 0 0

我需要编写一个高效的方法,如果矩阵中有一个十字,则返回k,如果没有,则返回-1。

我正在考虑在以下设计中编写方法:

循环遍历矩阵对角线中的所有数字,检查0。 如果找到0,我检查行中所有数字的总和,看它是否等于0。 如果是,我检查是否列中所有数字的总和,并检查它是否等于矩阵的长度。

如果是,则返回k。如果不是,则返回-1。 我不确定我的解决方案的复杂程度如何。如果这是O(n ^ 2)我没有得到所有的积分。如果有人能告诉我,我所建议的内容是否足够高效(以及它的高效程度),我会很高兴。

帮助会非常受欢迎。 :)

2 个答案:

答案 0 :(得分:2)

现在可能有点晚了,但编辑引起了我的注意。

这个问题让我想起了名人问题。我们可以观察到最多可能有一个十字架。所以,我们可以开始以下程序:

  1. 使用交叉候选项初始化堆栈(初始堆栈是从1到列计数的序列(或0到列数 - 1)。在进行任何检查之前,所有这些数字可能是交叉)
  2. 只要堆栈中有两个元素,就会迭代
    1. 从堆栈中选择两个最顶层的元素,让我们称它们为ij
    2. 检查i行和j列中的条目。如果条目为0,则j不能是十字(因为j - 列必须只包含1)。如果条目为1,则i不能是十字架
    3. 将一名候选人放回仍然可以作为十字架的堆叠
  3. 现在我们只有一名候选人,需要检查它是否真的是十字架(通过检查其行和列)。
  4. 整体时间复杂度为O(n)

答案 1 :(得分:0)

复杂性似乎是O(n ^ 2)。 getMatrix()方法中所有不同的矩阵值组合都返回正确的值。

    public static void main(String[] args) {
    int[][] matrix = getMatrix();
    int rows = matrix.length;
    int columns = matrix[0].length;
    int choiceRow=-1; 

    //Parse matrix row by row.
    for(int i=0;i<rows;i++)
    {           
        for(int j=0;j<columns;j++)
        {
            //on the first column
            if(j==0 && matrix[i][0] == 0)
            {
                //there is a chance this can be the choiceRow
                choiceRow=i;
            }
            else if(j==0 && matrix[i][j] == 1)
            {
                //if first column in current row is 1, no need to traverse row
                break;
            }
            //If current column is not first column, and current row is selected as
            //choiceRow, and current element is not 0, then reset choiceRow
            else if(j>0 && matrix[i][j] != 0 && choiceRow ==i)
            {
                //reset choiceRow, because this cannot be the choiceRow as it has an 
                //entry that is 1
                choiceRow = -1;
                //No point traversing further
                break;
            }
            //Else traverse further, no change in choiceRow required if all other 
            //columns in current row are 0.
        }
        ///If current row is truly choiceRow, then check for values in current column 
        //if they're all 1 except at matrix[i][k]
        if(choiceRow ==i)
        {
            //Iterate over choiceRow column             
            for(int k=0;k<rows;k++)
            {
                //If current row == current column, then skip comparison
                if(k==choiceRow)
                    continue;
                //If current row in selected column has a non-1 value, then reset 
                //choiceRow to -1
                if(matrix[k][choiceRow] != 1)
                {                           
                    choiceRow = -1;
                }
            }

            //If choiceRow == i , ie, it is not -1, then stop iterating 
            //and return choiceRow to o/p 
            if(choiceRow !=-1)
                break;
        }
    }

    System.out.println("ChoiceRow = "+choiceRow);
}

public static int[][] getMatrix()
{
//  int[][] matrix = {{1,0,1},{0,1,1},{0,0,0}};
//  int[][] matrix = {{1,0,1,0}, {1,0,1,0}, {0,0,0,0}, {1,0,1,1}};
//  int[][] matrix = {{1,0,1,1}, {1,0,0,1}, {1,0,1,1}, {0,0,0,1}};
    int[][] matrix = {{1,0,1,1}, {1,0,0,1}, {1,0,1,1}, {0,0,0,0}};      
    return matrix;
}