栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

LeetCode刷题python之335. 路径交叉

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode刷题python之335. 路径交叉

给你一个整数数组 distance 。

从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南移动 distance[2] 米,向东移动 distance[3] 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。

判断你所经过的路径是否相交。如果相交,返回 true ;否则,返回 false 。

示例 1:


输入:distance = [2,1,1,2]
输出:true
示例 2:


输入:distance = [1,2,3,4]
输出:false
示例 3:


输入:distance = [1,1,1,1]
输出:true
 

提示:

1 <= distance.length <= 105
1 <= distance[i] <= 105

from typing import List
class Solution:
    def isSelfCrossing(self, x: List[int]) -> bool:
        n = len(x)
        if n < 3: return False
        for i in range(3, n):
            if x[i] >= x[i - 2] and x[i - 3] >= x[i - 1]: 
                return True
            if i >= 4 and x[i - 3] == x[i - 1] and x[i] + x[i - 4] >= x[i - 2]: 
                return True
            if i >= 5 and x[i - 3] >= x[i - 1] and x[i - 2] >= x[i - 4] and x[i - 1] + x[i - 5] >= x[i - 3] and x[i] + x[i - 4] >= x[i - 2]: 
                return True
        return False

需要模拟多个样例发现其中的规律,可以发现都可以将其转换为以下三种情况:① 步数为四步以内相交 ② 步数在五步之内相交 ③ 步数在六步之内相交

路径交叉

力扣https://leetcode-cn.com/problems/self-crossing/solution/lu-jing-jiao-cha-by-leetcode-solution-dekx/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号