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
相关推荐
至乐活着3 小时前
深入Python内存管理与垃圾回收:从引用计数到分代回收的实战解析
python·性能优化·内存管理·垃圾回收·引用计数
m0_547486663 小时前
《Python数据分析与挖掘》全套PPT课件
python·数据挖掘·数据分析
㱘郳4 小时前
飞机大战Pygame存档
python·pygame
阿童木写作5 小时前
跨马翻译:一站式图片视频智能处理,提升跨境电商工作效率
人工智能·python·音视频
Python图像识别-15 小时前
基于yolov8的钢铁缺陷检测系统-2027毕业版(UI界面+Python项目源码+模型+标注好的数据集)
开发语言·python·yolo
aqi005 小时前
15天学会AI应用开发(十五)使用LangChain封装AI执行链
人工智能·python·大模型·ai编程·ai应用
想吃火锅10055 小时前
【leetcode】5.最长回文子串js
算法·leetcode·职场和发展
CoderYanger5 小时前
A.每日一题:1967题+1331题
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
kisbad5 小时前
Day 012|Embedding 和向量数据库:知识库检索到底在检什么
数据库·python·embedding·agent