Problem:You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south,x[3] 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(1) extra space to determine, if your path crosses itself, or not.

Example 1:

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

Example 2:

Given x = [1, 2, 3, 4],
┌──────┐
│ │


└────────────> Return false (not self crossing)

Example 3:

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

这题做得很狼狈啊呜哇啊啊啊!

思路来得蛮快的,因为cross的情况一共就三种,4弯cross,5个弯cross和6个弯cross:

#4个弯          5个弯            6个弯

  1              1                1
┌───┐ ┌───┐ ┌───┐
│2 │0 │2 │0 │ │0
└───┼> │ ↑ │2 │←┐
3 └───┘5 │ 5 │4
4 └─────┘
3

根据上图写出三种情况的判断式,这里错了好几次,第三种情况总是少了条件= - =

Code:

def isSelfCrossing(self, x):
l = (len(x))
iscross = False
if l < 4: return False
for i in range(3, l):
#情况1
if x[i-3]>=x[i-1] and x[i-2]<=x[i]:
return True
#情况2
if i>=4 and x[i-4]+x[i]>=x[i-2] and x[i-3]==x[i-1]:
return True
#情况3
if i>=5 and x[i-5]+x[i-1]>=x[i-3] and x[i-4]+x[i]>=x[i-2] and x[i-2]>=x[i-4] and x[i-2]>x[i-4] and x[i-3]>x[i-5] and x[i-1]<x[i-3]:
return True
iscross = False
return iscross

很粗暴的解法,第三种写了辣么长,不过好歹AC了。

围观别人家的代码:

class Solution(object):
def isSelfCrossing(self, x):
return any(d >= b > 0 and (a >= c or a >= c-e >= 0 and f >= d-b)
for a, b, c, d, e, f in ((x[i:i+6] + [0] * 6)[:6]
for i in xrange(len(x))))

class Solution(object):
def isSelfCrossing(self, x):
n = len(x)
x.append(0.5) # let x[-1] = 0.5
if n < 4: return False
grow = x[2] > x[0] for i in range(3,n):
if not grow and x[i] >= x[i-2]: return True
if grow and x[i] <= x[i-2]:
grow = False
if x[i] + x[i-4] >= x[i-2]:
x[i-1] -= x[i-3]
return False

对python的一些用法还是不够熟悉,毕竟第一门学的是C,写出来总是看着很繁琐。相比其他语言,python的代码都能缩到超短,多加练习啦~

【LeetCode】335. Self Crossing(python)的更多相关文章

  1. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  2. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. 【LeetCode】Algorithms 题集(三)

    Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...

  4. 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)

    描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...

  5. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  6. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

  7. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

随机推荐

  1. Picture POJ - 1177 (扫描线)

    扫描线求周长,可以看成两条线,一条扫x轴,一条扫y轴,然后这两天线扫过去的 周长加起来,就是周长了 #include<map> #include<set> #include&l ...

  2. centos7安装java环境和maven环境

    Java   官方下载:http://www.oracle.com/technetwork/java/javase/downloads/index.html Maven官方下载:http://mave ...

  3. JetBrains全家桶破解思路(最新更新:2019-04-17)

    JetBrains全家桶破解思路(以DataGrip为例) 2019-04-17 add new key 2019-01-24 add new key and code 2018-12-24 add ...

  4. 简明的Python教程中的几个疑惑点分析#2

    #1简明的Python教程当中第十四章Python标准库介绍中的使用sys模块 假如你没看懂sys模块这一章节,那么没关系,看下面一段代码再看简明的Python教程中的sys模块实例你将很容易看懂 代 ...

  5. jackson json转对象 对象转json

    一,Jackson使用示例 第1步:创建ObjectMapper对象. 创建ObjectMapper对象.它是一个可重复使用的对象. ObjectMapper mapper = new ObjectM ...

  6. C# Winfrom 发送邮件验证码&Timer控件

    邮件发送: //定义一个全局的string类型的验证码: string yzm = ""; //定义一个字符串,这里面包含所有需要的验证码的元素: string a = " ...

  7. 数据库 价格字段 设置 decimal(8,2),价格为100W,只显示999999.99

    DECIMAL(M,D),M是数字最大位数,D是小数点右侧数字个数,整数M-D位 decimal(8,2)数值范围是 -999999.99 ~ 999999.99 1000000超过了6位,严格模式下 ...

  8. rest_framework学习之路

    目录 RESTful理解 APIView 序列化组件 视图组件 解析器 认证组件 权限组件 频率组件 分页器 响应器 URL控制器 版本控制器

  9. jsp+servlet+poi导出数据库中的数据

    index.jsp: <a href="POIout">简单导出数据</a> <a href="POIoutTemplate"&g ...

  10. hibernate的学习周

    Hibernate核心:ORM(对象关系映射) BeginSession关闭的时候要session.close(),而getCurrentsession不需要,它会自动关闭 Session.load( ...