检查行反转后矩阵是否保持不变
给定一个 NxN 矩阵。任务是检查在反转给定矩阵的所有行后,矩阵是否保持不变。
例子:
Input : N = 3
1 2 1
2 2 2
3 4 3
Output : Yes
If all the rows are reversed then matrix will become:
1 2 1
2 2 2
3 4 3
which is same.
Input : N = 3
1 2 2
2 2 2
3 4 3
Output : No
方法:
- 一个最重要的观察是矩阵在行反转后是相同的,每一行都必须是回文的。
- 现在检查一行是否是回文,维护两个指针,一个指向开始,另一个指向行尾。开始比较存在的值并执行 start++ 和 end–。重复这个过程,直到所有元素都被检查到行的中间。如果每个步骤的元素都相同,则该行是回文,否则不是。
- 如果任何一行不是回文,那么答案是否定的。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
#define ll long long int
using namespace std;
// Function to check Palindromic Condition
void specialMatrix(int matrix[3][3], int N)
{
for (int i = 0; i < N; i++) {
// Pointer to start of row
int start = 0;
// Pointer to end of row
int end = N - 1;
while (start <= end) {
// Single Mismatch means row is not palindromic
if (matrix[i][start] != matrix[i][end]) {
cout << "No" << endl;
return;
}
start++;
end--;
}
}
cout << "Yes" << endl;
return;
}
// Driver Code
int main()
{
int matrix[3][3] = { { 1, 2, 1 },
{ 2, 2, 2 },
{ 3, 4, 3 } };
int N = 3;
specialMatrix(matrix, N);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to check Palindromic Condition
static void specialMatrix(int matrix[][], int N)
{
for (int i = 0; i < N; i++)
{
// Pointer to start of row
int start = 0;
// Pointer to end of row
int end = N - 1;
while (start <= end)
{
// Single Mismatch means
// row is not palindromic
if (matrix[i][start] != matrix[i][end])
{
System.out.println("No");
return;
}
start++;
end--;
}
}
System.out.println("Yes");
return;
}
// Driver Code
public static void main(String[] args)
{
int matrix[][] = { { 1, 2, 1 },
{ 2, 2, 2 },
{ 3, 4, 3 } };
int N = 3;
specialMatrix(matrix, N);
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python 3 implementation of the above approach
# Function to check Palindromic Condition
def specialMatrix(matrix, N):
for i in range(N):
# Pointer to start of row
start = 0
# Pointer to end of row
end = N - 1
while (start <= end):
# Single Mismatch means row is not palindromic
if (matrix[i][start] != matrix[i][end]):
print("No")
return
start += 1
end -= 1
print("Yes")
return
# Driver Code
if __name__ == '__main__':
matrix = [[1, 2, 1], [2, 2, 2], [3, 4, 3]]
N = 3
specialMatrix(matrix, N)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to check Palindromic Condition
static void specialMatrix(int [,]matrix, int N)
{
for (int i = 0; i < N; i++)
{
// Pointer to start of row
int start = 0;
// Pointer to end of row
int end = N - 1;
while (start <= end)
{
// Single Mismatch means
// row is not palindromic
if (matrix[i, start] != matrix[i, end])
{
Console.WriteLine("No");
return;
}
start++;
end--;
}
}
Console.WriteLine("Yes");
return;
}
// Driver Code
public static void Main(String[] args)
{
int [,]matrix = { { 1, 2, 1 },
{ 2, 2, 2 },
{ 3, 4, 3 } };
int N = 3;
specialMatrix(matrix, N);
}
}
// This code has been contributed by 29AjayKumar
PHP
Javascript
输出:
Yes