You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the north, then x[] metres to the west, x[] metres to the south, x[] metres to the east and so on. In other words, after each move your direction changes counter-clockwise.

Write a one-pass algorithm with O() extra space to determine, if your path crosses itself, or not.

Example :
Given x =
[, , , ]
,
┌───┐
│ │
└───┼──>
│ Return true (self crossing)
Example :
Given x =
[, , , ]
,
┌──────┐
│ │


└────────────> Return false (not self crossing)
Example :
Given x =
[, , , ]
,
┌───┐
│ │
└───┼> Return true (self crossing)

题目说有一串数x,是一些移动轨迹,从逆时针往北开始走过x[i]长度,说走过的这些轨迹会有相交吗?

一开始以为就四个数,然后判断一波就行了,没想到这里面有坑,x是不定长数组,因此不是走四次就结束了,是要把数组都判断一遍才可以。

对于这个问题,可以这么考虑,分为len(x)<4,len(x)==4,len(x)>4的。

len(x)<4 显然不相交;

len(x)==4, 只有x[2]<=x[0] && x[3]>=x[1]才有可能相交;

len(x)>4时,

我们考虑相交的相反情况,我们找不相交的情况,不相交主要有两种状态,外旋跟内旋,看下图

初始判断进入哪个状态是在x[3]决定,x[3]>x[1]->状态1,x[3]<x[1]->状态2,x[3]==x[1]时比较特殊,要再判断一下x[4]+x[0]>=x[2]是否成立,成立则相交,否则进入状态2。

情况1就是外旋,为了保持外旋的状态,x[i]需要大于x[i-2],由情况3可以看出来,若x[i]<=x[i-2],那么下一个x[i](如果存在),要么变成内旋,要么就相交,所以除非x[i]>x[i-2]能一致保持外旋的状态,否则就要进入中间状态,这里称为状态3。

状态3是为了判断相交或进入状态2而准备的,也就是说在情况3中x[i==6]这步由于x[6]<x[4]所以要进入中间状态了,而且这里要注意根据x[6]的长度,若x[6]+x[2]<x[4]那么下一刻x[7]若相交则只会跟x[4]相交,x[7]必须>x[5],而若x[6]+x[2]>=x[4]那么x[7]+x[3]>x[5]就会导致相交,若不相交,则x[8]开始就会进入状态2。

情况2中,内旋判断x[i]是否会相交只要判断x[i]>x[i-2]成立,就相交。

因此总结起来:

if len(x) <4 then false

if len(x)==4 && x[0] > 0 and x[2] <= x[0] and x[1] > 0 and x[3] >= x[1] then true

if len(x)>5:

  当前状态1:若x[i]<=x[i-2]那么根据x[i]长度进入不同的状态3

  当前状态2:若x[i]>=x[i-2]=>相交

  当前状态3:根据状态3的两种不同情况和x[i]长度判断是相交还是进入状态2

代码如下:

 class Solution(object):
def isSelfCrossing(self, x):
"""
:type x: List[int]
:rtype: bool
"""
if (len(x) < 4):
return False
if (x[0] > 0 and x[2] <= x[0] and x[1] > 0 and x[3] >= x[1]):
return True
if (len(x) < 5):
return False
statu = 0
juage = -1
if (x[3] > x[1]):
statu = 1
if (x[3] < x[1]):
statu = 2
if (x[3] == x[1]):
if(x[4] + x[0]) >= x[2]:
return True
else:
statu = 2
for i in range(4, len(x)):
if statu == 1:
if (x[i] <= x[i - 2]):
statu = 0
if (x[i - 4] + x[i] >= x[i - 2]):
juage = 1
continue
else:
juage = 2
continue if statu == 2:
if (x[i] >= x[i - 2]):
return True
if juage != -1:
if juage == 1:
if (x[i] + x[i - 4] < x[i - 2]):
statu = 2
juage = -1
else:
return True
if juage == 2:
if (x[i] < x[i - 2]):
statu = 2
juage = -1
else:
return True
return False

[leetcode]335. Self Crossing的更多相关文章

  1. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  2. 335. Self Crossing

    /* * 335. Self Crossing * 2016-7-10 by Mingyang */ // Categorize the self-crossing scenarios, there ...

  3. 【LeetCode】Self Crossing(335)

    1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[ ...

  4. 【LeetCode】335. Self Crossing(python)

    Problem:You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metr ...

  5. Java实现 LeetCode 335 路径交叉

    335. 路径交叉 给定一个含有 n 个正数的数组 x.从点 (0,0) 开始,先向北移动 x[0] 米,然后向西移动 x[1] 米,向南移动 x[2] 米,向东移动 x[3] 米,持续移动.也就是说 ...

  6. Leetcode 335.路径交叉

    路径交叉 给定一个含有 n 个正数的数组 x.从点 (0,0) 开始,先向北移动 x[0] 米,然后向西移动 x[1] 米,向南移动 x[2] 米,向东移动 x[3] 米,持续移动.也就是说,每次移动 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. kafka 分区和副本以及kafaka 执行流程,以及消息的高可用

    1.Kafka概览 Apache下的项目Kafka(卡夫卡)是一个分布式流处理平台,它的流行是因为卡夫卡系统的设计和操作简单,能充分利用磁盘的顺序读写特性.kafka每秒钟能有百万条消息的吞吐量,因此 ...

  2. Mac 下配置Lua环境

    1.下载LUA源码包 curl -R -O http://www.lua.org/ftp/lua-5.3.1.tar.gz 2.解压并进入目录 tar -xvf lua-.tar.gz cd lua- ...

  3. TestLink测试管理工具的使用举例—第一篇

    本博客用来详细说明TestLink测试管理工具的使用方法,前两篇博客已经详细说明了TestLink工具的下载,安装及基本登录功能,本篇开始说明其工具的具体使用! 下载安装TestLink工具之后,我们 ...

  4. 微信连wifi,中文ssid报Invalid sign tosign错误

    Invalid sign tosign错误如上: 是微信官方接受和回传的问题,改固定字符解决: 比如将ssid固定修改为字符串‘ssid’即可:

  5. Fedora 23 U盘启动出现“Failed to load ldlinux.c32”解决 ZT

      利用UltraISO制作了Fedora 23的U盘启动,开机F12键USB启动时出现 1 2 Failed to load ldlinux.c32 Boot failed: please chan ...

  6. 网络爬虫之网站图片爬取-python实现

    版本1.5 本次简单添加了四路多线程(由于我电脑CPU是四核的),速度飙升.本想试试xPath,但发现反倒是多此一举,故暂不使用 #-*- coding:utf-8 -*- import re,url ...

  7. 莫烦tensorflow(8)-CNN

    import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 dat ...

  8. linux ipv6开启的配置文件

    1./etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetUUID=9d1d6e2a-cfc5-4e60-8f28-b77 ...

  9. php批量检测和去掉bom头(转)

    <?php //有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码 if (isset ( $_GET ['dir'] )) { //confi ...

  10. 仿QQ菜单栏:消息,电话菜单

    转载自:http://blog.csdn.net/johnnyz1234/article/details/45919907 在实际项目开发使用Fragment的时候,也碰到一些异常和存在的问题,下面做 ...