原文:

给定一个尺寸为 n * marr[][] ,任务是以顺时针形式打印给定矩阵的边界元素。

示例:

输入: arr[][] = {{1,2,3},{4,5,6},{7,8,9} } 输出: 1 2 3 6 9 8 7 4 说明: 矩阵的边界元素有: 1 2 3 t13】456 t18<强

输入: arr[][] = {{11,12,33},{64,57,61},{74,88,39}} 输出: 11 12 33 61 39 88 74 64

天真法:解决这个问题最简单的方法是。如果发现为真,则打印该元素。

时间复杂度:o(n2) 辅助空间: o(1)

高效途径:优化上述途径,思路是只遍历矩阵的首末行首末列。按照以下步骤解决问题:

  • 打印矩阵的第一行。
  • 打印矩阵除第一行以外的最后一列。
  • 打印矩阵的最后一行,除了最后一列。
  • 打印矩阵的第一列,第一行和最后一行除外。

下面是上述方法的实现:

c

// c   program of the above approach
#include 
using namespace std;
// function to print the boundary elements
// of the matrix in clockwise
void boundarytraversal(vector > arr, int n,
                       int m)
{
  // print the first row
  for (int i = 0; i < m; i  )
  {
    cout << arr[0][i] << " ";
  }
  // print the last column
  // except the first row
  for (int i = 1; i < n; i  )
  {
    cout << arr[i][m - 1] << " ";
  }
  // print the last row
  // except the last column
  if (n > 1)
  {
    // print the last row
    for (int i = m - 2; i >= 0; i--)
    {
      cout << arr[n - 1][i] << " ";
    }
  }
  // print the first column except
  // the first and last row
  if (m > 1) {
    // print the first column
    for (int i = n - 2; i > 0; i--) {
      cout << arr[i][0] << " ";
    }
  }
}
// driver code
int main()
{
  vector > arr{ { 1, 2, 3 },
                           { 4, 5, 6 },
                           { 7, 8, 9 } };
  int n = arr.size();
  int m = arr[0].size();
  // function call
  boundarytraversal(arr, n, m);
  return 0;
}
// this code is contributed by dharanendra l v

java 语言(一种计算机语言,尤用于创建网站)

// java program of the above approach
import java.util.*;
class gfg {
    // function to print the boundary elements
    // of the matrix in clockwise
    public static void boundarytraversal(
        int arr[][], int n, int m)
    {
        // print the first row
        for (int i = 0; i < m; i  ) {
            system.out.print(arr[0][i]   " ");
        }
        // print the last column
        // except the first row
        for (int i = 1; i < n; i  ) {
            system.out.print(arr[i][m - 1]   " ");
        }
        // print the last row
        // except the last column
        if (n > 1) {
            // print the last row
            for (int i = m - 2; i >= 0; i--) {
                system.out.print(arr[n - 1][i]   " ");
            }
        }
        // print the first column except
        // the first and last row
        if (m > 1) {
            // print the first column
            for (int i = n - 2; i > 0; i--) {
                system.out.print(arr[i][0]   " ");
            }
        }
    }
    // driver code
    public static void main(string[] args)
    {
        int arr[][]
            = { { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 } };
        int n = arr.length;
        int m = arr[0].length;
        // function call
        boundarytraversal(arr, n, m);
    }
}

python 3

# python program of the above approach
# function to print the boundary elements
# of the matrix in clockwise
def boundarytraversal(arr, n, m):
    # print the first row
    for i in range(m):
        print(arr[0][i], end = " ");
    # print the last column
    # except the first row
    for i in range(1, n):
        print(arr[i][m - 1], end = " ");
    # print the last row
    # except the last column
    if (n > 1):
        # print the last row
        for i in range(m - 2, -1, -1):
            print(arr[n - 1][i], end = " ");
    # print the first column except
    # the first and last row
    if (m > 1):
        # print the first column
        for i in range(n - 2, 0, -1):
            print(arr[i][0], end = " ");
# driver code
if __name__ == '__main__':
    arr = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]];
    n = len(arr);
    m = len(arr[0]);
    # function call
    boundarytraversal(arr, n, m);
    # this code is contributed by 29ajaykumar

c

// c# program of the above approach
using system;
class gfg{
// function to print the boundary elements
// of the matrix in clockwise
static void boundarytraversal(int[,] arr,
                              int n, int m)
{
    // print the first row
    for(int i = 0; i < m; i  )
    {
        console.write(arr[0, i]   " ");
    }
    // print the last column
    // except the first row
    for(int i = 1; i < n; i  )
    {
        console.write(arr[i, m - 1]   " ");
    }
    // print the last row
    // except the last column
    if (n > 1)
    {
        // print the last row
        for(int i = m - 2; i >= 0; i--)
        {
            console.write(arr[n - 1, i]   " ");
        }
    }
    // print the first column except
    // the first and last row
    if (m > 1)
    {
        // print the first column
        for(int i = n - 2; i > 0; i--)
        {
            console.write(arr[i, 0]   " ");
        }
    }
}
// driver code   
static void main()
{
    int[,] arr = { { 1, 2, 3 },
                   { 4, 5, 6 },
                   { 7, 8, 9 } };
    int n = 3;
    int m = 3;
    // function call
    boundarytraversal(arr, n, m);
}
}
// this code is contributed by divyeshrabadiya07

java 描述语言


output: 

1 2 3 6 9 8 7 4

时间复杂度: o(n m) 辅助空间: o(1)