【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. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
随机推荐
- Zabbix-proxy安装部署
原文链接:https://www.cnblogs.com/irockcode/p/6754659.html 环境: 因为公司需要监控远程客户机,但server端无法主动连接agent端,客户端可以连接 ...
- Python3 与 C# 面向对象之~继承与多态
2.继承¶ 代码裤子:https://github.com/lotapp/BaseCode 在线编程:https://mybinder.org/v2/gh/lotapp/BaseCode/mast ...
- Jupyter Notebook 编辑器美化
汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai 2018-10-10新增Linux样式路径: Logo: ~/anaconda3/lib ...
- Sublime text 3支持utf-8
首先安装插件ConvertToUTF8和Codecs33 Sublime Text 3中文乱码问题解决(最新) 然后配置一些有用的用户设置 2. 使文档保存时自动存为 UTF-8 编码格式 默认情况下 ...
- C++ template一些体悟(1)
#include <iostream> using namespace std; template<typename T> class testClass { public: ...
- 定制kickstart重建CentOS7.5镜像用于U盘引导安装
有什么问题或者疑惑,可以留言,全力解答. ISO 镜像制作 U 盘安装盘 U盘安装CentOS7.4 U盘实现CentOS7.3全自动安装系统 https://www.cnblogs.com/pany ...
- curl与wget
curl 和wget 区别 使用方法 可参考 curl vs Wget 1.curl是libcurl这个库支持的,wget是一个纯粹的命令行命令.2.curl支持更多的协议.curl supports ...
- qml: 以鼠标为中心进行放缩;
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true widt ...
- bzoj2819 DFS序 + LCA + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2819 题意:树上单点修改及区间异或和查询. 思维难度不高,但是题比较硬核. 整体思路是维护每一个结 ...
- 关于java文件下载文件名乱码问题解决方案
JAVA文件下载时乱码有两种情况: 1,下载时中文文件名乱码 2,下载时因为路径中包含中文文件名乱码,提示找不到文件 解决方法见下面部分代码 response.setContentType(" ...