作者介绍:10年大厂数据\经营分析经验,现任大厂数据部门负责人。
会一些的技术:数据分析、算法、SQL、大数据相关、python
欢迎加入社区:码上找工作
作者专栏每日更新:
LeetCode解锁1000题: 打怪升级之旅
python数据分析可视化:企业实战案例
给定一个包含 m x n
个元素的矩阵(m
行, n
列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
top
, bottom
, left
, right
。def spiralOrder(matrix):
"""
按顺时针螺旋顺序返回矩阵中的所有元素
:param matrix: List[List[int]], 输入的矩阵
:return: List[int], 螺旋顺序的元素列表
"""
if not matrix:
return []
result = []
top, bottom, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
while True:
# Traverse from left to right.
for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1
if top > bottom: break
# Traverse downwards.
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1
if left > right: break
# Traverse from right to left.
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1
if top > bottom: break
# Traverse upwards.
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
if left > right: break
return result
# 示例调用
print(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) # 输出: [1,2,3,6,9,8,7,4,5]
m
是矩阵的行数,n
是矩阵的列数,我们需要遍历矩阵中的每个元素。def spiralOrder(matrix):
"""
分层模拟法按顺时针螺旋顺序返回矩阵中的所有元素
:param matrix: List[List[int]], 输入的矩阵
:return: List[int], 螺旋顺序的元素列表
"""
result = []
while matrix:
result += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
result.append(row.pop())
if matrix:
result += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
result.append(row.pop(0))
return result
# 示例调用
print(spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])) # 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
def spiralOrder(matrix):
"""
递归剥离法按顺时针螺旋顺序返回矩阵中的所有元素
:param matrix: List[List[int]], 输入的矩阵
:return: List[int], 螺旋顺序的元素列表
"""
return matrix and [*matrix.pop(0)] + spiralOrder([*zip(*matrix)][::-1])
# 示例调用
print(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) # 输出: [1,2,3,6,9,8,7,4,5]
def spiralOrder(matrix):
"""
迭代加标记法按顺时针螺旋顺序返回矩阵中的所有元素
:param matrix: List[List[int]], 输入的矩阵
:return: List[int], 螺旋顺序的元素列表
"""
if not matrix:
return []
rows, cols = len(matrix), len(matrix[0])
seen = [[False] * cols for _ in range(rows)]
result = []
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # right, down, left, up
r = c = di = 0 # Start with the top-left corner going right
for _ in range(rows * cols):
result.append(matrix[r][c])
seen[r][c] = True
nr, nc = r + directions[di][0], c + directions[di][1]
if 0 <= nr < rows and 0 <= nc < cols and not seen[nr][nc]:
r, c = nr, nc
else:
di = (di + 1) % 4 # Change direction
r, c = r + directions[di][0], c + directions[di][1]
return result
# 示例调用
print(spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) # 输出: [1,2,3,6,9,8,7,4,5]
特征 | 方法一: 模拟路径 | 方法二: 分层模拟 | 方法三: 递归剥离 | 方法四: 迭代加标记 |
---|---|---|---|---|
时间复杂度 | (O(mn)) | (O(mn)) | (O(mn)) | (O(mn)) |
空间复杂度 | (O(1)) | (O(1)) | (O(m + n)) | (O(mn)) |
优势 | - 直观简单 | - 实现易于理解 | - 简洁的代码 | - 避免递归 |
劣势 | - 需要多个循环 | - 需要多次旋转矩阵 | - 高空间复杂度 | - 需要额外空间标记 |
在图像处理中,螺旋遍历可以用于实现各种图像变换效果,如螺旋形状的模糊、放大、缩小或像素重组。这种遍历方式可以帮助模拟自然现象中的螺旋图案,或在艺术处理中创造特定的视觉效果。
螺旋模糊效果:
像素重组:
以螺旋模糊为例,下面是一段简化的Python代码,展示如何实现螺旋遍历并逐步增加模糊处理:使用任意一张图片即可进行实验
from PIL import Image, ImageFilter
def spiralBlur(image_path):
image = Image.open(image_path)
pixels = image.load()
width, height = image.size
result = Image.new('RGB', (width, height))
result_pixels = result.load()
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # right, down, left, up
x, y, di = 0, 0, 0
seen = set()
for _ in range(width * height):
result_pixels[x, y] = pixels[x, y]
seen.add((x, y))
nx, ny = x + directions[di][0], y + directions[di][1]
if (nx, ny) not in seen and 0 <= nx < width and 0 <= ny < height:
x, y = nx, ny
else:
di = (di + 1) % 4
x, y = x + directions[di][0], y + directions[di][1]
# Apply increasing blur
local_radius = min(width, height) // 2
distance = ((x - width // 2)**2 + (y - height // 2)**2)**0.5
blur_radius = int((distance / local_radius) * 10) # Example blur increase
small_region = image.crop((x-1, y-1, x+2, y+2))
blurred_region = small_region.filter(ImageFilter.GaussianBlur(radius=blur_radius))
result.paste(blurred_region, (x-1, y-1))
result.save('spiral_blurred_output.jpg')
# Usage
spiralBlur('path_to_image.jpg')
在机器人路径规划中,尤其是在自动清扫机器人和搜索救援机器人的应用中,螺旋遍历可以系统地覆盖搜索区域。这种路径规划确保每一部分区域都能被访问,同时效率高于简单的来回扫描。
自动清扫机器人:
搜索救援机器人:
机器人路径规划:
在机器人科技中,螺旋矩阵遍历可模拟机器人在二维空间的路径规划,特别是在需要系统地覆盖一个区域进行搜索或清扫时。救援机器人可能需要在废墟中搜索生还者。使用螺旋路径可以系统地覆盖搜寻区域,提高搜索效率和成功率。
假设一个简单的基于网格的模型,下面是一段示例代码,表示如何规划路径:
def spiralPath(width, height):
path = []
x, y = 0, 0
dx, dy = 0, 1 # Initial direction: move right
while True:
path.append((x, y))
if not (0 <= x + dx < width and 0 <= y + dy < height):
dx, dy = dy, -dx # Change direction
if (x + dx, y + dy) in path:
break
x, y = x + dx, y + dy
return path
# Usage example for a 5x5 area
path = spiralPath(5, 5)
print(path)
通过以上两个应用示例,我们可以看到螺旋矩阵遍历算法不仅理论上有趣,而且在实际应用中具有广泛的用途,特别是在需要系统覆盖和处理的场景中。
如果对你有帮助,给一个三连哦 谢谢大家!