简单的乐趣#59:反转对角线【难度:1级】--景越Python编程训练之挑战1000道Python面试题(含答案)

简单的乐趣#59:反转对角线【难度:1级】:

#任务

给定一个正方形“矩阵”,你的任务是颠倒两个最长对角线上元素的顺序。

方阵的最长对角线定义如下:
*第一个最长的对角线从左上角到右下角;
*第二长的对角线从右上角到左下角。

#例子

对于矩阵

1,2,3
4,5,6
7,8,9

输出应该是:

9,2,7
4,5,6
3,8,1

# 输入输出

  • [input]2D整数数组矩阵

约束:1≤matrix.length≤10,matrix.length = matrix [i] .length,1≤matrix[i] [j]≤1000

  • [输出]2D整数数组

矩阵与其最长对角线上的元素顺序相反。

英文原题:

Task

Given a square matrix, your task is to reverse the order of elements on both of its longest diagonals.

The longest diagonals of a square matrix are defined as follows:

  • the first longest diagonal goes from the top left corner to the bottom right one;
  • the second longest diagonal goes from the top right corner to the bottom left one.

Example

For the matrix

1, 2, 3
4, 5, 6
7, 8, 9

the output should be:

9, 2, 7
4, 5, 6
3, 8, 1

Input/Output

  • [input] 2D integer array matrix

    Constraints: 1 ≤ matrix.length ≤ 10, matrix.length = matrix[i].length, 1 ≤ matrix[i][j] ≤ 1000

  • [output] 2D integer array

    Matrix with the order of elements on its longest diagonals reversed.

最佳答案合集(多种解法):

点击查看答案

更多关联题目:

Krazy King二十一点【难度:3级】–景越Python编程训练之挑战1000道Python面试题(含答案)
单词搜索网格【难度:3级】–景越Python编程训练之挑战1000道Python面试题(含答案)
简单的乐趣#31:替换密码?【难度:2级】–景越Python编程训练之挑战1000道Python面试题(含答案)

免责申明:

本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢

你可能感兴趣的:(简单的乐趣#59:反转对角线【难度:1级】--景越Python编程训练之挑战1000道Python面试题(含答案))