Algorithm 写一种有效的方法
我被指示写一个方法,在一个正方形矩阵中找到一个“交叉点”(这个问题中的所有矩阵都由1和0组成) 如果第k行中的所有元素都是0,第k列中的所有元素都是1(除了[k][k]中等于0的元素外),则k是一个交叉点 例如,2在此矩阵中是一个叉:Algorithm 写一种有效的方法,algorithm,optimization,matrix,Algorithm,Optimization,Matrix,我被指示写一个方法,在一个正方形矩阵中找到一个“交叉点”(这个问题中的所有矩阵都由1和0组成) 如果第k行中的所有元素都是0,第k列中的所有元素都是1(除了[k][k]中等于0的元素外),则k是一个交叉点 例如,2在此矩阵中是一个叉: 1 01 01 0 0 0 我需要写一个efficent方法,如果矩阵中有一个十字,它返回k,如果没有十字,它返回-1 我正在考虑在以下设计中编写一个方法: 遍历矩阵对角线中所有数字的循环,检查0。 如果找到0,我检查行中所有数字的总和,看它是否等于0。 如果是,
1 01
01
0 0 0 我需要写一个efficent方法,如果矩阵中有一个十字,它返回k,如果没有十字,它返回-1 我正在考虑在以下设计中编写一个方法: 遍历矩阵对角线中所有数字的循环,检查0。 如果找到0,我检查行中所有数字的总和,看它是否等于0。 如果是,我检查列中所有数字的和,并检查它是否等于矩阵的长度 如果是,则返回k。如果不是,则返回-1。 不过,我不确定我的解决方案的复杂程度。如果这是O(n^2),我不会得到所有的分数。如果有人能告诉我我所建议的是否足够有效(以及它的有效水平是多少),我会很高兴
非常感谢您的帮助。:) 现在可能有点晚了,但编辑引起了我的注意 这个问题让我想起了名人问题。我们可以观察到,最多可能有一个十字架。因此,我们可以开始以下程序:
i
和j
i
-th行和j
-th列中的条目。如果条目为0,j
不能是十字(因为j
-th列只能包含1s)。如果条目为1,i
不能是十字 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;
}
publicstaticvoidmain(字符串[]args){
int[][]矩阵=getMatrix();
int行=矩阵长度;
int columns=矩阵[0]。长度;
int choiceRow=-1;
//逐行分析矩阵。
对于(int i=0;i任何一种特定的编程语言?矩阵数据类型是否需要是二维的?如果主对角线都是零(可能是零),那么在计算每列总计和每行总计时,必须读取每个单元格。这显然是O(n^2)其中矩阵为n*n。另请参见什么是交叉候选项
?堆栈如何用它们“初始化”?如回答中所述:堆栈以从1到列计数的顺序初始化。对于4列,初始堆栈为1、2、3、4
。这些数字中的每一个都可能是任何检查前的交叉。是的,好点:)。这是我想到的第一个解决方案。作为获取choiceRow(在上面)的更好方法,我只能通过查看对角线位置为0的行来获取choiceRow,但在最坏的情况下,这不会提高复杂性。。。