【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]
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)的更多相关文章
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【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 ...
- 【LeetCode】Algorithms 题集(三)
Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...
- 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)
描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
随机推荐
- 【UNR #1】火车管理
题目描述 uoj 旗下有一个火车站,用来管理属于 uoj 的小火车. 火车站一共有 nn 条编号为 1,…,n1,…,n 的,只有一端的用来存放小火车的铁路,由于小火车特殊的构造,每条铁路可以停放无数 ...
- react案例->新闻移动客户端--(react+redux+es6+webpack+es6的spa应用)
今天分享一个react应用,应在第一篇作品中说要做一个react+redux+xxx的应用.已经做完一部分,拿出来分享.github地址为:点我就可以咯~ 这里实现了一个新闻移动站的spa.本来想写p ...
- nodejs的某些api~(六)HTTPS
node的HTTPS模块接口与HTTP其实差不多,就是多了一个认证证书,私钥的配置等等,API都相似的. 在客户端服务器通信的方法中,只有HTTPS是最安全的,它的原理是客户端和服务器发送自己的公钥, ...
- [bzoj3524][Couriers]
题目链接 思路 观察这个\((r - l + 1)/2\),很容易证明,如果一个数出现次数大于\((r - l + 1) / 2\),那么这个区间内第\((r - l + 1) / 2 + 1\)大一 ...
- 好用的漂浮广告 jquery
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.js" type="text/javascript ...
- HTML学习笔记Day4
一.浮动属性 1.首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流: 无论多么复杂的布局,其基本出发点均是:“如何在一行显示多个div元素”: 显然标准流已经无法满足需求 ...
- Windows下MySQL下载安装、配置与使用
用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. (我这个写得有点太详细了,甚至 ...
- Luogu P4306 [JSOI2010]连通数 传递闭包
正解其实是\(Tarjan\) + \(拓扑拓扑\),但是却可以被\(O(N^3 / 32)\)复杂度的传递闭包水过去.心疼一下写拓扑的小可爱们. 学到一个\(bitset\)优化布尔图的骚操作,直接 ...
- 苹果中国全系降价:iphone最高降500元,用户可退差价
本文首发于:风云社区SCOEE(社区旨在普惠软件.图片.音乐.视频.素材.文档等互联网资源.为大众提供多样化的服务,以及主要涵盖学术科学.电脑技术.文化人文.体育健身等领域的知识和信息,获得用户的支持 ...
- NoClassDefFoundError com/google/inject/Injector
一个maven项目莫名其妙的遇上了NoClassDefFoundError com/google/inject/Injector,在maven-surefire-plugin插件中配置 了<fo ...