程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Two dimensional array related topics force deduction Python

編輯:Python

Two dimensional array related topics Power button Python

    • 48. Rotated image
    • 73. Matrix zeroing
    • 498. Diagonal traversal
    • 118. Yang hui triangle
    • 119. Yang hui triangle II
    • 54. Spiral matrix
    • 59. Spiral matrix II
    • 289. Life game

48. Rotated image

Their thinking :
solution 1. Main diagonal inversion plus vertical inversion .

solution 2. Horizontal inversion plus main diagonal inversion .

class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
""" Do not return anything, modify matrix in-place instead. """
n = len(matrix)
for i in range(n // 2):
for j in range(n):
matrix[i][j], matrix[n-i-1][j] = matrix[n-i-1][j], matrix[i][j]
# Reverse diagonally 
for i in range(n):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

73. Matrix zeroing

Their thinking :

The problem requires constant space and is solved in situ .

Use the first row and the first column of the matrix to mark 0 Rows and columns of .

  1. Set two variables to record whether each row or column should be set 0.
  2. First traverse the first row and the first column of the matrix .
  3. Then traverse the matrix divided by the first row and the first column .
  4. First place 0 A matrix divided by the first row and the first column .
  5. Repositioning 0 The matrix of the first row and the first column .
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
""" Do not return anything, modify matrix in-place instead. """
m = len(matrix)
n = len(matrix[0])
flag_col0 = False
flag_row0 = False
# Traverse the first line 
for i in range(m):
if matrix[i][0] == 0:
flag_col0 = True
break
# Traverse the first column 
for j in range(n):
if matrix[0][j] ==0:
flag_row0 = True
break
# Use the first row and the first column , Record the rest of the matrix as 0 Rows and columns of 
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == 0:
matrix[i][0] = matrix[0][j] = 0
# Set up 0
for i in range(1, m):
for j in range(1, n):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if flag_col0:
for i in range(m):
matrix[i][0] = 0
if flag_row0:
for j in range(n):
matrix[0][j] = 0

498. Diagonal traversal

Their thinking :

  1. The odd column moves up and to the right , Even columns move down and to the right .
  2. Move up and to the right :
    Traversal direction x - 1, y + 1;
    The boundary problem : Encounter the first line ,y + 1; Encounter the last column ,x + 1
  3. Move down and left :
    Traversal direction x + 1, y - 1;
    The boundary problem : Encounter the last line ,y + 1; Encounter the first column ,x + 1
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m, n = len(mat), len(mat[0])
count = m * n
x,y= 0,0
res = []
for i in range(count):
res.append(mat[x][y])
# Move to the top right 
if(x + y) % 2 ==0:
# The last column 
if y == n - 1:
x += 1
# first line 
elif x== 0:
y += 1
else:
x -= 1
y += 1
# Move lower left 
else:
# The last line 
if x == m - 1:
y += 1
# First column 
elif y ==0:
x += 1
else:
x += 1
y -= 1
return res

118. Yang hui triangle

Their thinking :
simulation
Both sides of the boundary are 1
The first i Elements of the line j For the first time i - 1 Elements of the line j - 1 and j And

class Solution:
def generate(self, numRows: int) -> List[List[int]]:
res = []
for i in range(numRows):
row = []
for j in range(i+1):
if j ==0 or j ==i:
row.append(1)
else:
row.append(res[i-1][j-1] + res[i-1][j])
res.append(row)
return res

119. Yang hui triangle II

Their thinking :
And 118 Same logic or simulation , The difference is to output the given index row .

Use an array , Double loop traversal .
First traversal k That's ok , Then the elements at each position of each line are assigned in reverse order .
The reverse order is because res[j] = res[j-1] + res[j] .

class Solution:
# simulation 
def getRow(self, rowIndex: int) -> List[int]:
res = [0] * (rowIndex + 1)
for i in range(rowIndex + 1):
for j in range(i, -1, -1):
if j==0 or j== i:
res[j] = 1
else:
res[j] = res[j-1] + res[j]
return res

54. Spiral matrix

Their thinking :
Set boundaries up, down, left, right , Simulate again .

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
up, down, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
res = []
while True:
for i in range(left, right + 1):
res.append(matrix[up][i])
up += 1
if up > down:
break
for i in range(up, down + 1):
res.append(matrix[i][right])
right -= 1
if right < left:
break
for i in range(right, left - 1, -1):
res.append(matrix[down][i])
down -= 1
if down < up:
break
for i in range(down, up - 1, -1):
res.append(matrix[i][left])
left += 1
if left > right:
break
return res

59. Spiral matrix II

Their thinking :
And 54 The same way of thinking .
No, this time, I want to define a matrix , Assign a value to the matrix clockwise .

class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
matrix = [[0 for _ in range(n)] for _ in range(n)]
up, down, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
index = 1
while True:
for i in range(left, right + 1):
matrix[up][i] = index
index += 1
up += 1
if up > down:
break
for i in range(up, down + 1):
matrix[i][right] = index
index += 1
right -= 1
if right < left:
break
for i in range(right, left - 1, -1):
matrix[down][i] = index
index += 1
down -= 1
if down < up:
break
for i in range(down, up - 1, -1):
matrix[i][left] = index
index += 1
left += 1
if left > right:
break
return matrix

289. Life game

Their thinking :

Living conditions :

  1. It turned out to be alive , There is 2-3 A living one , Be alive
  2. It was dead , There is 3 A living one , Be alive
  3. Everything else is dead

Use living cells to affect other grids , Instead of traversing eight grids around each cell in turn .

Add the number in the affected grid 10, The original state of the one digit passbook grid , Ten people keep how many living grids are around it .

For example, a grid is 31, It means that there is... Around it 3 Live cells , And the previous status is 1, That is, live .

The problem requires to be solved in situ , After judging all the grids , And then change it uniformly according to the living conditions borad.

class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
""" Do not return anything, modify board in-place instead. """
m=len(board)
n=len(board[0])
# Use each living point to affect other points 
def affect( x, y):
for i in [x-1, x, x+1]:
for j in [y-1, y, y+1]:
if i < 0 or i >= m or j<0 or j>=n or(i==x and j ==y):
continue
board[i][j] += 10
def calculate(x, y):
value = board[x][y]
if value // 10 == 3:
board[x][y] = 1
elif value // 10 ==2 and value % 10 == 1:
board[x][y] = 1
else:
board[x][y] = 0
for i in range(m):
for j in range(n):
if board[i][j] % 10 == 1:
affect(i,j)
for i in range(m):
for j in range(n):
calculate(i,j)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved