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
相关推荐
Sakura_j_h_1 小时前
从零写一个 MCP stdio Server:用 Python 校验博客文章格式
开发语言·python·自动化·ai工具·mcp·json-rpc
大草原的小灰灰2 小时前
LLM应用的标准开发框架LangChain(八)- 中间件
python·中间件·langchain
卷无止境2 小时前
百万 token 时代的真相:主流大模型上下文窗口与最大输出的横向对比分析
人工智能·python
用户298698530142 小时前
Python 实战:将 HTML 批量转换为 Markdown
python·html·markdown
文丑颜不良啊2 小时前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊2 小时前
LeetCode2321~2340题目解析
java·leetcode
hhcgchpspk3 小时前
flask服务端页面预防被爬措施
后端·python·flask·网络爬虫·防爬
小小张说故事3 小时前
Python 里 copy.copy() 和 deepcopy() 到底差在哪:拆开 __copy__、__deepcopy__ 和那个防止无限递归的 memo 字典
python
vipxieliang3 小时前
Python collections:高效容器与实战案例
后端·python
青少儿编程课堂3 小时前
蓝桥杯青少组图形化 STEMA 省考(报名倒计时·11/22开考)+ 全国青少年信息素养大赛 2026—2027赛季 + 电子学会图形化考级:校园华容道大闯关
c++·python·算法·bfs·信息学竞赛