📜  给定矩阵中的回文行数

📅  最后修改于: 2022-05-13 01:57:22.719000             🧑  作者: Mango

给定矩阵中的回文行数

给定一个大小为N * N的矩阵arr[][] ,任务是找出回文行的数量。

例子

方法:可以使用两点方法来解决该任务。请按照以下步骤操作:

  • 遍历矩阵的每一行。
  • 对于每一行:
    • 使用两个指针指向行的开头和行的结尾。
    • 如果两个指针的值相同,则增加起始指针并减少结束指针。
    • 继续这样做,直到两个指针都指向同一个元素或它们具有不同的值。
    • 如果它们具有不同的值,则该行不是回文。停止迭代并返回 false。否则继续下一行。
  • 在遍历所有行并且每个人都是回文之后返回 true。

下面是该方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
const int MAX = 100;
 
// Function to count the number of
// palindromic rows
int countPalindrome(
    vector >& arr,
    int N)
{
    int count = 0;
    for (int i = 0; i < N; i++) {
        int j = 0, k = N - 1;
        bool t = true;
        while (j < k) {
            if (arr[i][j] != arr[i][k]) {
                t = false;
                break;
            }
            j++;
            k--;
        }
        if (t)
            count++;
    }
    return count;
}
 
// Driver Code
int main()
{
    int N = 3;
    vector > arr
        = { { 1, 3, 1 }, { 2, 2, 3 }, { 2, 1, 2 } };
    cout << countPalindrome(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class Solution {
    // Function to count the number of
    // palindromic rows
    static int countPalindrome(int arr[][],
                               int N)
    {
        int count = 0;
        for (int i = 0; i < N; i++) {
            int j = 0, k = N - 1;
            boolean t = true;
            while (j < k) {
                if (arr[i][j] != arr[i][k]) {
                    t = false;
                    break;
                }
                j++;
                k--;
            }
            if (t)
                count++;
        }
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 3;
        int arr[][]
            = { { 1, 3, 1 }, { 2, 2, 3 }, { 2, 1, 2 } };
        System.out.println(
            countPalindrome(arr, N));
    }
}


Python3
# Python program for the above approach
MAX = 100;
 
# Function to count the number of
# palindromic rows
def countPalindrome(arr, N):
    count = 0;
    for i in range(N):
        j = 0
        k = N - 1
        t = True
        while (j < k):
            if (arr[i][j] != arr[i][k]):
                t = False
                break;
            j += 1
            k -= 1
        if (t):
            count += 1
    return count;
 
# Driver Code
N = 3;
arr = [[1, 3, 1], [2, 2, 3], [2, 1, 2]];
print(countPalindrome(arr, N));
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
 
public class GFG{
 
  // Function to count the number of
  // palindromic rows
  static int countPalindrome(int[,] arr,
                             int N)
  {
    int count = 0;
    for (int i = 0; i < N; i++) {
      int j = 0, k = N - 1;
      bool t = true;
      while (j < k) {
        if (arr[i, j] != arr[i, k]) {
          t = false;
          break;
        }
        j++;
        k--;
      }
      if (t)
        count++;
    }
    return count;
  }
 
  // Driver code
  static public void Main (){
 
    int N = 3;
    int[,] arr = new int[3, 3] { { 1, 3, 1 }, { 2, 2, 3 }, { 2, 1, 2 } };
    Console.WriteLine(
      countPalindrome(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
2

时间复杂度:O(N*N)
辅助空间:O(1)