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. Dividing the Path POJ - 2373(单调队列优化dp)

    给出一个n长度的区间,然后有一些小区间只能被喷水一次,其他区间可以喷水多次,然后问你要把这个区间覆盖起来最小需要多少喷头,喷头的半径是[a, b]. 对于每个只能覆盖一次的区间,我们可以把他中间的部分 ...

  2. IncDec Sequence(差分)

    题意:给定一个序列,可以对一个区间进行加1或减1的操作,问最少需要多少次可以将序列的值一样. Solution 我们将序列差分,得到一个差分数组. 对于每一个区间操作,我们可以把它转化为在查分数组上某 ...

  3. scrapy 选择器

    1.lxml(转自简书) from lxml import etree2 import requests3 45 url = " "6 html = requests.get(ur ...

  4. CF1153F Serval and Bonus Problem

    Serval and Bonus Problem 1.转化为l=1,最后乘上l 2.对于一个方案,就是随便选择一个点,选在合法区间内的概率 3.对于本质相同的所有方案考虑在一起,贡献就是合法区间个数/ ...

  5. 【CF1119D】Frets On Fire

    题目大意:给定一个长度为 n 的序列,给定一个恒定的 w,求解 \[\sum\limits_{i=1}^{n}min\{d[i],w\}\] 题解:学会了对最小值和式的快速处理. 若在下标的角度考虑, ...

  6. Mybatis 批量插入时得到插入的id(mysql)

    前言: 在开发中,我们可能很多的时候可能需要在新增时得到刚才新增的id,后续的逻辑需要用到这个id. 在插入单条记录的情况下,这个是很简单的问题.多条记录时有个坑在里面. 单条记录的代码如下 < ...

  7. ImageMagick - 设置透明带 AlphaChannel 的 png 图片的透明度

    学习和使用ImageMagick也有几年时间了,其实对 ImageMagick 的了解还仅仅是皮毛吧. 最近在写代码的时候,遇到一个调整透明png图片透明度的问题: 当1张png图片,有些区域是透明的 ...

  8. 工具类:Colletions ,Arrays(静态导入,可变参数,强循环)

    一.Collecti 专门用来操作集合的工具类,没有构造函数,全静态方法. 常用方法: static <T extends Comparable<? super T>> voi ...

  9. tensor flow中summary用法总结

    对于用法的总结详细的参见博文https://www.cnblogs.com/lyc-seu/p/8647792.html

  10. mac host文件配置

    Shift+Command+G 三个组合按键,并输入 Hosts 文件的所在路径:/etc/hosts /private/etc/hosts