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
相关推荐
wabs6661 分钟前
关于动态规划【力扣1143.最长公共子序列的思考】
算法·leetcode·动态规划
剑挑星河月22 分钟前
54.螺旋矩阵
java·算法·leetcode·矩阵
zh路西法26 分钟前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Vodka~1 小时前
WSL2 + RViz GPU渲染机械臂
人工智能·python
8Qi81 小时前
hello-agents学习笔记--Memory让Agent拥有记忆
人工智能·python·llm·agent·ai编程·vibecoding
Esaka_Forever2 小时前
Python 完整内存管理机制详解
开发语言·python·spring
笨笨没好名字2 小时前
Leetcode刷题python3版第一周(下)
linux·算法·leetcode
Weigang2 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源
小九九的爸爸2 小时前
前端入门Agent开发,掌握这些Python数据基础就够啦
python·agent