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
相关推荐
小小张说故事4 小时前
Seaborn 入门指南:让统计数据可视化更优雅
python·机器学习
小小张说故事4 小时前
Transformers 入门指南:用 Python 调用预训练大模型
python·机器学习
小小张说故事4 小时前
FastAPI 入门指南:用 Python 极速构建 API
python·机器学习
小小张说故事4 小时前
pytest 入门指南:Python 自动化测试的标配工具
python·机器学习
用户8356290780514 小时前
使用 Python 在 Excel 中添加和管理图片
后端·python
用户3169353811834 小时前
SQLAlchemy 的两套 API
python
伞伞悦读4 小时前
【第45期】一行 eval 做计算器的代价:Python 输入校验和可测试的猜数字
python
用户8356290780514 小时前
Python 设置 Excel 单元格边框与样式
后端·python
10年前端老司机4 小时前
LLM降本提速三档对比:无缓存、普通缓存、语义缓存(LangChain生产落地)
人工智能·python·langchain
用户298698530144 小时前
Python 文档格式转换实战:RTF 转 PDF 与 HTML
python·html·api