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
相关推荐
Tisfy11 分钟前
LeetCode 3531.统计被覆盖的建筑:最大最小值
算法·leetcode·题解·桶排序
道199321 分钟前
PyTorch 从小白到高级进阶教程[工业级示例](三)
人工智能·pytorch·python
月明长歌30 分钟前
【码道初阶】Leetcode.189 轮转数组:不熟悉ArrayList时踩得坑,被Arraylist初始化骗了?
java·算法·leetcode·职场和发展
测试人社区-千羽33 分钟前
智能测试的终极形态:从自动化到自主化的范式变革
运维·人工智能·python·opencv·测试工具·自动化·开源软件
fantasy_arch34 分钟前
leetcode算法-最大乘积子数组
算法·leetcode·职场和发展
锐学AI39 分钟前
从零开始学MCP(八)- 构建一个MCP server
人工智能·python
木棉知行者40 分钟前
PyTorch 核心方法:state_dict ()、parameters () 参数打印与应用
人工智能·pytorch·python
xingzhemengyou141 分钟前
python time的使用
python
码界奇点1 小时前
基于Python与GitHub Actions的正方教务成绩自动推送系统设计与实现
开发语言·python·车载系统·自动化·毕业设计·github·源代码管理
长安er1 小时前
LeetCode876/141/142/143 快慢指针应用:链表中间 / 环形 / 重排问题
数据结构·算法·leetcode·链表·双指针·环形链表