73. 矩阵置0
python
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 简单的做法,使用一个标记矩阵,但是这样过于繁琐
# 我们可以仅仅使用一个行向量数组和列向量数组进行标记
m,n = len(matrix),len(matrix[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
row[i] = col[j] = 1
for i in range(m):
for j in range(n):
if row[i] or col[j]:
matrix[i][j] = 0
return matrix
时间复杂度:O(mn)
空间复杂度:O(m+n)
54. 螺旋矩阵
python
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
# 一圈一圈转即可,有点儿递归的思想
left = 0
right = len(matrix[0])
top = 0
bottom = len(matrix)
res = []
# 当矩阵只有一行时,我们遍历了上,右会跳过,但是下还会遍历一遍,就会重复
# 当矩阵只有一列时,我们遍历了上和右,但是左还会遍历,也会重复
# 所以上和右是一定要遍历的,左和下要判断
while top<bottom and left<right:
for i in range(left,right):
res.append(matrix[top][i])
top += 1
for j in range(top,bottom):
res.append(matrix[j][right-1])
right -= 1
if top< bottom:
for i in reversed(range(left,right)):
res.append(matrix[bottom-1][i])
bottom -= 1
if left< right:
for j in reversed(range(top,bottom)):
res.append(matrix[j][left])
left +=1
return res
时间复杂度:O(mn)
空间复杂度:O(1)
48. 旋转图像
python
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 数学上可证明,顺时针旋转90就等价于先水平翻转,然后转置
n = len(matrix)
if n<2:
return
for i in range(n//2):
matrix[i],matrix[n-i-1] = matrix[n-i-1],matrix[i]
for i in range(n):
for j in range(i+1,n):
matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j]
return
python
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 思路清奇但是好理解的方法:
# 上下左右各取4个点,旋转90,然后位置++ 直到外层结束
n = len(matrix)
left = top = 0
right = bottom = n-1
for e in range(n//2):
for i in range(n-2*e-1):
# 一次性交换有问题,注意交换顺序
# 先保存上
tmp = matrix[top][left+i]
# 左到上
matrix[top][left+i] = matrix[bottom-i][left]
# 下到左
matrix[bottom-i][left] = matrix[bottom][right-i]
# 右到下
matrix[bottom][right-i] = matrix[top+i][right]
# 上到右
matrix[top+i][right] = tmp
left +=1
top +=1
bottom -=1
right -=1
时间复杂度:O(nn)
空间复杂度:O(1)
240. 搜索二维矩阵 II
python
class Solution(object):
def binary_search(self, nums, target):
left = 0
right = len(nums)-1
while left <= right:
mid = (left+right) // 2
if target == nums[mid]:
return True
elif target > nums[mid]:
left = mid+1
else:
right = mid-1
return False
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
# 每一行用二分查找
for nums in matrix:
if self.binary_search(nums,target):
return True
return False
时间复杂度:O(mlogn)
空间复杂度:O(1)
但是以上的做法没有用到列的单调信息。
python
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
# 从左往右递增,从上往下递增
# 我们应该先尽快锁定要查找的列或者行
# 而且我们希望仅仅通过一个元素的比较,就可以较快的舍弃
# 右上角的元素a
# 如果a大于target,那么这一列都会大于a,这一列舍弃
# 如果a小于target,那么这一行都会小于a,这一行舍弃
m = len(matrix)
n = len(matrix[0])
i = 0
j = n -1
while i<m and j >=0:
if matrix[i][j] == target:
return True
if matrix[i][j] > target:
j -=1
if matrix[i][j] < target:
i +=1
return False
时间复杂度:O(m+n)
空间复杂度:O(1)