LeetCode 867 Transpose Matrix 解题报告

题目要求

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over it‘s main diagonal, switching the row and column indices of the matrix.

题目分析及思路

题目要求得到矩阵的转置矩阵。可先得到一个行数与原矩阵列数相等、列数与原矩阵行数相等的矩阵,再对原矩阵进行遍历。

python代码?

class Solution:

def transpose(self, A: ‘List[List[int]]‘) -> ‘List[List[int]]‘:

rows, cols = len(A), len(A[0])

res = [[0] * rows for _ in range(cols)]

for row in range(rows):

for col in range(cols):

res[col][row] = A[row][col]

return res

原文地址:https://www.cnblogs.com/yao1996/p/10352518.html

时间: 2024-08-29 12:36:30

LeetCode 867 Transpose Matrix 解题报告的相关文章

【Golang语言版本】LeetCode 867. Transpose Matrix 矩阵转置

矩阵转置,A[i][j] 变成A[j][i] 比较简单,直接上代码了. func transpose(A [][]int) [][]int { B := make([][]int, len(A[0])) for i := 0; i < len(A[0]); i++ { B[i] = make([]int, len(A)) for j := 0; j < len(A); j++ { B[i][j] = A[j][i] } } return B } 原文地址:https://blog.51cto.

867. Transpose Matrix - LeetCode

Question 867. Transpose Matrix Solution 题目大意:矩阵的转置 思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可 Java实现: public int[][] transpose(int[][] A) { int[][] B = new int[A[0].length][A.length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[0].length; j++

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

【Leetcode_easy】867. Transpose Matrix

problem 867. Transpose Matrix 参考1. Leetcode_easy_867. Transpose Matrix; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11215040.html

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl