867. Transpose Matrix - Day 2/30 Leetcode June Challenge

preview_player
Показать описание
Larry solves and analyzes this Leetcode problem as both an interviewer and an interviewee. This is a live recording of a real engineer solving a problem live - no cuts or edits!

#leetcode #coding #programming
Рекомендации по теме
Комментарии
Автор

Hi Larry, I have the similar solution as yours:
class Solution:
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
res = [[0 for _ in range(m)] for _ in range(n)]
for i in range(m):
for j in range(n):
res[j][i] = matrix[i][j]
return res

yan-vnoy
Автор

Hey Larry! I tried initializing the ans in two ways but your way of using a for loop inside comprehension works, but this : ans = [[0]*R]*C doesn't. This gives wrong answer with the last elements of each row and column. why?

satwikdas
visit shbcf.ru