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. [CQOI2017]老C的方块

    题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=4823 题解 观察那四种条件 有没有什么特点? 我们可以把蓝线两边的部分看做两个区域,这样 ...

  2. 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)

    链接:https://www.nowcoder.com/acm/contest/197/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  3. Markdown基础(内含:锚点使用,使用HTML,新页面跳转,目录生成)

    Github样式显示参考:点我 之前说过用word写文章,这次说说Markdown写文章(推荐) 逆天推荐使用VSCode编写 装这个插件写作更方便: 内含:锚点使用,使用HTML,新页面跳转,目录生 ...

  4. 洛谷P3810 陌上花开 CDQ分治(三维偏序)

    好,这是一道三维偏序的模板题 当然没那么简单..... 首先谴责洛谷一下:可怜的陌上花开的题面被无情的消灭了: 这么好听的名字#(滑稽) 那么我们看了题面后就发现:这就是一个三维偏序.只不过ans不加 ...

  5. numpy 多维数组及数组操作

    NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Numpy内部解除了Python的PIL(全局解释器锁),运算效率极好,是大量机 ...

  6. How MVC pattern Flows

    以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)

  7. laravel基于redis实现的一个简单的秒杀系统

    说明:网上很多redis秒杀系统的文章,看的都是一头雾水,然后自己来实现一个,也方便以后自己学习 实现的方式是用的redis的list队列,框架为laravel 核心部分为list的pop操作,此操作 ...

  8. Solr7.1--- 指定ConfigSets

    我们都知道Solr提供了一个默认的配置,以及一个demo 但是实际场景是我们需要自己独立配置,不用默认的,主要为了方便管理. 进入目录: D:\solr-7.1.0\server\solr\confi ...

  9. KMP之计算Next数组

    KMP的Next数组:模式串的前缀与后缀的“相交”长度 KMP算法步骤: 1.先算next数组 2.若失配(此时模式串下标为j),利用Next数组求出失配后滑动的新位置 a.Next[j] \geq ...

  10. 阿里面试:MYSQL的引擎区别

    MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...