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
相关推荐
坚持编程的菜鸟6 分钟前
LeetCode每日一题——螺旋矩阵
c语言·算法·leetcode·矩阵
(●—●)橘子……12 分钟前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
GalaxyPokemon15 分钟前
LeetCode - 1171.
算法·leetcode·链表
nueroamazing43 分钟前
PPT-EA:PPT自动生成器
vue.js·python·语言模型·flask·大模型·项目·ppt
iナナ43 分钟前
Java优选算法——位运算
java·数据结构·算法·leetcode
一壶浊酒..1 小时前
python 爬取百度图片
开发语言·python·百度
该用户已不存在1 小时前
工具用得好,Python写得妙,9个效率工具你值得拥有
后端·python·编程语言
广龙宇1 小时前
【一起学Rust · 项目实战】使用getargs库来获取命令行参数
开发语言·python
Han.miracle2 小时前
数据结构二叉树——层序遍历&& 扩展二叉树的左视图
java·数据结构·算法·leetcode
mit6.8242 小时前
[Agent开发平台] 后端的后端 | MySQL | Redis | RQ | idgen | ObjectStorage
人工智能·python