335_self_crossing

原题python

    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.算法

Example1shell

Given x = [2, 1, 1, 2],
┌───┐
│         |
└───┼──>
          │
Return true (self crossing)

 Example2数组

Given x = [1, 2, 3, 4],
┌──────┐
│                │
│
│
└────────────>

Return false (not self crossing)

   Example3测试

Given x = [1, 1, 1, 1],
┌───┐
│        │
└───┼>

Return true (self crossing)

  简介spa

    一看到这个题目的时候,我暗自窃喜,这题目也太简单了吧,只须要x[2]的长度大于等于x[0],x[3]的长度大于等于x[1]的长度。另外题目给出的路径只有x[0],x[1],x[2],x[3],再将题目中的三个example,带入测试一下,应该就没问题了。然而写完,run code步骤执行经过之后,commit代码的时候的错误才让我意识到,个人思路是如此的浅薄,主要浅薄的点有两点。code

1) 认为x这个数组长度为四,其实题目中已经给出array的长度为n个正整数;orm

2)只要知足x[0] > x[2] , x[3] > x[1],就能判断其路径为self crossing,然而,这种判断只表明了这个路径分类中的一种状况。开发

分析

1)将路径的交叉状况进行分类

状况一:

    n > 3的状况下, 只要x[n-1] > x[n-3] , x[n-2] < x[i-4];

解释:第四条线和第一条线交叉,或者第五条线和第二条线交叉,以此类推。

状况二:

    n > 4的状况下,只要x[n-1] + x[n-5] > x[n-3] , x[n-2] == x[n-4];

解释 : 第五条线和第一条线重叠,或者第六条线和第二条线重叠,以此类推。

状况三:

    n > 5的状况下,只要x[n-5] + x[n-1] > x[n-3] , x[n-2] + x[n-6] > x[n-4] , x[n-3] > x[n-5], x[n-4] > x[n-2];

解释:第六条线和第一条线交叉,或者第七条线和第二条线交叉,以此类推。

代码以下

class Solution(object):
    def isSelfCrossing(self, x):
        """
        :type x: List[int]
        :rtype: bool
        """
        length = len(x)
        for i in range(3,length):
            """fourth line crosses first line and works for fifth line crosses second line and so on"""
            if i <= length - 1 and x[i] >= x[i-2] and x[i-1] <= x[i-3]:   #状况一
                return True
            if i >= 4:   #状况二
                if i <= length -1 and x[i-1] == x[i-3] and x[i] + x[i-4] >= x[i-2]:
                    return True
            if i >= 5:   #状况三
                if i <= length -1 and x[i-4] + x[i] >= x[i-2] and x[i-1] + x[i-5] >=x[i-3] and x[i-3] >= x[i-1] and x[i-2] >= x[i-4]:  
                    return True
        return False

总结

对于这类算法,在咱们不断的写错,不断的接受test case和coner case的考验,咱们才会进一步的去完善咱们的分类讨论的思路,另外对于解决这类问题,看似代码量不大,可是在没有理清思路的状况下,仍是很容易写出又臭又长的代码的。

    之后解决这种题目的时候,我要好好的梳理本身的思路,尽可能不轻易的写下思路不明确的代码。只有这样才能告别case by case的开发模式,而且成为一名合格的程序员。

本站公众号
   欢迎关注本站公众号,获取更多信息