LeetCode题解(0885):螺旋矩阵III(Python)

题目:原题链接(中等)

标签:数组、数学

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( R × C ) O(R×C) O(R×C) O ( 1 ) O(1) O(1)116ms (80.00%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def spiralMatrixIII(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]:
        # 定义边缘位置
        top, bottom, left, right = r0, r0, c0, c0 + 1
        stat = 0
        x, y = r0, c0

        ans = []

        while len(ans) < R * C:
            if 0 <= x < R and 0 <= y < C:
                ans.append([x, y])
            if stat == 0:  # 向右移动的状态
                if y < right:
                    y += 1
                else:
                    bottom += 1
                    stat = 1
                    x += 1
            elif stat == 1:  # 向下移动的状态
                if x < bottom:
                    x += 1
                else:
                    left -= 1
                    stat = 2
                    y -= 1
            elif stat == 2:  # 向左移动的状态
                if y > left:
                    y -= 1
                else:
                    top -= 1
                    stat = 3
                    x -= 1
            else:  # 向上移动的状态
                if x > top:
                    x -= 1
                else:
                    right += 1
                    stat = 0
                    y += 1

        return ans
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值