给定一个N * N维的平方矩阵M[ ][ ] ,任务是找到矩阵的第K对角线,从左上角到右下角,并打印其内容。
例子:
Input: N = 3, K = 2, M[ ][ ] = {{4, 7, 8}, {9, 2, 3}, {0, 4, 1}
Output: 9 7
Explanation:
5 possible diagonals starting from the top left to the bottom right for the given matrix are:
1 -> {4}
2 -> {9, 7}
3 -> {0, 2, 8}
4 -> {4, 3}
5 -> {1}
Since K = 2, the required diagonal is (9, 7).
Input: N = 2, K = 2, M[ ][ ] = {1, 5}, {9, 4}}
Output: 1
Explanation:
3 possible diagonals starting from the bottom left to the top right for the given matrix are:
1 -> {1}
2 -> {9, 5}
3 -> {4}
Since K = 2, the required diagonal is (9, 5).
天真的方法:
解决此问题的最简单方法是生成从给定矩阵的左上角到右下角的所有对角线。产生第K个对角后,打印的内容。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:
可以通过避免矩阵的遍历来优化上述方法。相反,找到的第K对角线即左下和右上指数的边界。获得这些索引后,只需遍历对角线的索引即可打印所需的对角线。
需要以下观察来找到对角线的位置:
If ((K – 1) < N): The diagonal is an upper diagonal
Bottom-left boundary = M[K – 1][0]
Top-right boundary = M[0][K – 1]
If (K ? N): The diagonal is a lower diagonal.
Bottom-left boundary = M[N-1][K-N]
Top-right boundary = M[K-N][N-1]
请按照以下步骤解决问题:
- 如果(K – 1) < N ,则将起始行startRow 设置为K – 1并将列startCol 设置为0 。
- 否则,将startRow设置为N – 1并将startCol 设置为K – N 。
- 最后,从M[startRow][startCol]开始打印对角线的元素。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function returns required diagonal
void printDiagonal(int K, int N,
vector >& M)
{
int startrow, startcol;
// Initialize values to
// print upper diagonals
if (K - 1 < N) {
startrow = K - 1;
startcol = 0;
}
// Initialize values to
// print lower diagonals
else {
startrow = N - 1;
startcol = K - N;
}
// Traverse the diagonal
for (; startrow >= 0 && startcol < N;
startrow--, startcol++) {
// Print its contents
cout << M[startrow][startcol]
<< " ";
}
}
// Driver Code
int main()
{
int N = 3, K = 4;
vector > M = { { 4, 7, 8 },
{ 9, 2, 3 },
{ 0, 4, 1 } };
printDiagonal(K, N, M);
return 0;
}
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
// Function returns required diagonal
static void printDiagonal(int K, int N,
int [][]M)
{
int startrow, startcol;
// Initialize values to
// print upper diagonals
if (K - 1 < N)
{
startrow = K - 1;
startcol = 0;
}
// Initialize values to
// print lower diagonals
else
{
startrow = N - 1;
startcol = K - N;
}
// Traverse the diagonal
for(; startrow >= 0 && startcol < N;
startrow--, startcol++)
{
// Print its contents
System.out.print(M[startrow][startcol] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 3, K = 4;
int[][] M = { { 4, 7, 8 },
{ 9, 2, 3 },
{ 0, 4, 1 } };
printDiagonal(K, N, M);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the
# above approach
def printDiagonal(K, N, M):
startrow, startcol = 0, 0
# Initialize values to prupper
# diagonals
if K - 1 < N:
startrow = K - 1
startcol = 0
else:
startrow = N - 1
startcol = K - N
# Traverse the diagonals
while startrow >= 0 and startcol < N:
# Print its contents
print(M[startrow][startcol], end = " ")
startrow -= 1
startcol += 1
# Driver code
if __name__ == '__main__':
N, K = 3, 4
M = [ [ 4, 7, 8 ],
[ 9, 2, 3 ],
[ 0, 4, 1 ] ]
printDiagonal(K, N, M)
# This code is contributed by mohit kumar 29
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function returns required diagonal
static void printDiagonal(int K, int N,
int [,]M)
{
int startrow, startcol;
// Initialize values to
// print upper diagonals
if (K - 1 < N)
{
startrow = K - 1;
startcol = 0;
}
// Initialize values to
// print lower diagonals
else
{
startrow = N - 1;
startcol = K - N;
}
// Traverse the diagonal
for(; startrow >= 0 && startcol < N;
startrow--, startcol++)
{
// Print its contents
Console.Write(M[startrow,startcol] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 3, K = 4;
int[,] M = { { 4, 7, 8 },
{ 9, 2, 3 },
{ 0, 4, 1 } };
printDiagonal(K, N, M);
}
}
// This code is contributed by PrinciRaj1992
输出:
4 3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live