人生苦短我用Python
虽然还是写了出来,但是还是查了答案的,看了别人的思路的
最终答案:
class Solution: def isSelfCrossing(self, list_1): n = len(list_1) if n <= 3: return False for i in range(3, n): if list_1[i] >= list_1[i-2] and list_1[i-3] >= list_1[i-1]: return True if i == 4: if list_1[i] >= (list_1[i-2] - list_1[i-4]) and list_1[i-1] == list_1[i-3]: return True if i >= 5: if list_1[i] >= list_1[i-2] - list_1[i-4] and list_1[i-2] > list_1[i-4] and list_1[i-1] >= list_1[i-3] - list_1[i-5] and list_1[i-3] >= list_1[i-1]: return True return False
既然是原创代码肯定还是写的有点坎坷的。
最离谱的是我把 and 写成了& ,我以为是一样的,但结果一直预测错误。
and是用于条件句里的“与”运算
&是位运算里的“与”运算
这两者并不是一个东西