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)

4th line may cross with 1st line, and so on: 5th with 2nd, ...etc

5th line may cross with 1st line, and so on: 6th with 2nd, ...etc

6th line also may cross with 1st line, and so on: 7th with 2nd, ...etc

However, if 7th line also cross with 1st line, either of the following cases should definitely happens:

  a. 7th line cross with 2nd line

  b. 6th line cross with 1st line

  we have covered these cases.

 public class Solution {
public boolean isSelfCrossing(int[] x) {
if (x.length <= 3) return false;
for (int i=3; i<x.length; i++) {
//check if 4th line cross with the first line and so on
if (x[i]>=x[i-2] && x[i-1]<=x[i-3]) return true; //check if 5th line cross with the first line and so on
if (i >= 4) {
if (x[i-1]==x[i-3] && x[i]+x[i-4]>=x[i-2]) return true;
} //check if 6th line cross with the first line and so on
if (i >= 5) {
if (x[i-2]>=x[i-4] && x[i]>=x[i-2]-x[i-4] && x[i-1]<=x[i-3] && x[i-1]>=x[i-3]-x[i-5]) return true;
}
}
return false;
}
}

Leetcode: Self Crossing的更多相关文章

  1. [LeetCode] Self Crossing 自交

    You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...

  2. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  3. 【LeetCode】Self Crossing(335)

    1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[ ...

  4. 【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] metr ...

  5. [leetcode]335. Self Crossing

    You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the n ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  7. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  8. Leetcode: Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. SET GLOBAL long_query_time=0

    SHOW VARIABLES LIKE '%long%'

  2. Machine Learning in Action -- 回归

    机器学习问题分为分类和回归问题 回归问题,就是预测连续型数值,而不像分类问题,是预测离散的类别 至于这类问题为何称为回归regression,应该就是约定俗成,你也解释不通 比如为何logistic ...

  3. Andrew Ng机器学习公开课笔记–Principal Components Analysis (PCA)

    网易公开课,第14, 15课 notes,10 之前谈到的factor analysis,用EM算法找到潜在的因子变量,以达到降维的目的 这里介绍的是另外一种降维的方法,Principal Compo ...

  4. nginx下php频繁卡死502

    解决:[WARNING] fpm_children_bury(), line 215: child 2736 (pool default) exited on signal 15 SIGTERM af ...

  5. this绑定

    js中关于this的用法,在初期时候经常会弄混,即使现在,也不敢说就一定不会混,但是起码好很多了. 函数执行过程中,主要有4种方法决定this的绑定对象. 分别为:默认绑定.隐式绑定. 显示绑定和ne ...

  6. php--validate表单验证

    validate表单验证扩展规则 添加自定义检验(验证class) 获取html加入 class <input id="D_NUMBER" name="D_NUMB ...

  7. 常用分类列表wp_list_categories()

    使用: <ul> <?php $args= array( 'depth'=>1, 'orderby'=>id, 'style'=>none ); wp_list_c ...

  8. ArcGIS Engine开发之旅04---ARCGIS接口详细说明

    原文:ArcGIS Engine开发之旅04---ARCGIS接口详细说明 ArcGIS接口详细说明... 1 1.      IField接口(esriGeoDatabase)... 2 2.    ...

  9. [LeetCode]题解(python):094 Binary Tree Inorder Traversal

    题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...

  10. sqlserver 中含有某字符串

    查找 sqlserver 中字符串的ascii码SET TEXTSIZE 0-- Create variables for the character string and for the curre ...