Python | Leetcode Python题解之第335题路径交叉

题目:

题解:

python 复制代码
class Solution:
    def isSelfCrossing(self, distance: List[int]) -> bool:
        n = len(distance)

        # 处理第 1 种情况
        i = 0
        while i < n and (i < 2 or distance[i] > distance[i - 2]):
            i += 1

        if i == n:
            return False

        # 处理第 j 次移动的情况
        if ((i == 3 and distance[i] == distance[i - 2])
                or (i >= 4 and distance[i] >= distance[i - 2] - distance[i - 4])):
            distance[i - 1] -= distance[i - 3]
        i += 1

        # 处理第 2 种情况
        while i < n and distance[i] < distance[i - 2]:
            i += 1

        return i != n
相关推荐
pursuit_csdn3 分钟前
力扣周赛 503
java·算法·leetcode
sheeta19988 分钟前
LeetCode 每日一题笔记 日期:2026.05.21 题目:3043. 最长公共前缀的长度
笔记·算法·leetcode
恣艺9 分钟前
Python 图像处理实战:Pillow 与 OpenCV 从入门到精通
图像处理·python·pillow
QCzblack10 分钟前
期中考复现
开发语言·python
心中有国也有家12 分钟前
PyTorch 适配 NPU:从 torch_npu 到 CANN 算子的全链路技术解析
人工智能·pytorch·python
盼小辉丶16 分钟前
PyTorch强化学习实战(10)——强化学习高级组件
人工智能·pytorch·python·强化学习
EntyIU22 分钟前
Python学习笔记
笔记·python·学习
sheeta199824 分钟前
LeetCode 每日一题笔记 日期:2026.05.23 题目:1752. 检查数组是否经排序和轮转得到
笔记·算法·leetcode
wuxinyan12324 分钟前
工业级大模型学习之路025:问题解决-检索质量全为0
人工智能·python·学习·langchain
weixin_4080996724 分钟前
2026 图片高清化 API 实战:AI超分辨率重建技术详解 + Python/Java/PHP/C#代码示例
图像处理·人工智能·python·超分辨率重建·石榴智能·图片变清晰·图片高清化api