美文网首页
335. Self Crossing

335. Self Crossing

作者: Jeanz | 来源:发表于2017-08-23 22:59 被阅读0次

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)

一刷
题解:
这道题给了我们一个一位数组,每个数字是个移动量,按照上左下右的顺序来前进每一个位移量,问我们会不会和之前的轨迹相交,而且限定了常量的空间复杂度。实际上相交的情况只有以下三种情况:

第一类是第四条边和第一条边相交的情况,需要满足的条件是第一条边大于等于第三条边,第四条边大于等于第二条边。同样适用于第五条边和第二条边相交,第六条边和第三条边相交等等,依次向后类推的情况...

     x(1)
    ┌───┐
x(2)│   │x(0)
    └───┼──>
    x(3)│

第二类是第五条边和第一条边重合相交的情况,需要满足的条件是第二条边和第四条边相等,第五条边大于等于第三条边和第一条边的差值,同样适用于第六条边和第二条边重合相交的情况等等依次向后类推...

      x(1)
    ┌──────┐
    │      │x(0)
x(2)│      ^
    │      │x(4)
    └──────│
      x(3)

第三类是第六条边和第一条边相交的情况,需要满足的条件是第四条边大于等于第二条边,第三条边大于等于第五条边,第五条边大于等于第三条边和第一条边的差值,第六条边大于等于第四条边和第二条边的差值,同样适用于第七条边和第二条边相交的情况等等依次向后类推...

      x(1)
    ┌──────┐
    │      │x(0)
x(2)│     <│────│
    │       x(5)│x(4)
    └───────────│
        x(3)

相关文章

  • 335. Self Crossing

    You are given an array x of n positive numbers. You start...

  • 8.19 - hard - 69

    335. Self Crossing 一道数学题,考虑一条边被cross的两种情况,然后依次顺延边。

  • Leetcode - Self Crossing

    My code: reference:https://discuss.leetcode.com/topic/380...

  • 【算法】Self Crossing 自我相交

    题目 You are given an array x of n positive numbers. You st...

  • 335.

    内隐和外显的区别是什么?是本质和表面的意思吗?有个ted视频提出了这点。 游泳时的摆腿,只是外显,而不是内隐。人游...

  • 335.

    寄希望于男人什么真的是很糟糕的事 男人总是会一次又一次的让女人失望

  • 推荐系统之Deep Crossing模型原理以及代码实践

    简介 本文要介绍的Deep Crossing模型是由微软研究院在论文《Deep Crossing: Web-Sca...

  • 飞鸟集 242

    This life is the crossing of a sea, where we meet in the ...

  • Atcoder(Crossing)

    链接:https://tenka1-2018.contest.atcoder.jp/tasks/tenka1_20...

  • faith

    In 1982 Steven Callahan was crossing the Atlantic alone i...

网友评论

      本文标题:335. Self Crossing

      本文链接:https://www.haomeiwen.com/subject/kgfddxtx.html