457. 环形数组是否存在循环
为保证权益,题目请参考 457. 环形数组是否存在循环(From LeetCode).
解决方案1
Python
python
# 457. 环形数组是否存在循环
# https://leetcode-cn.com/problems/circular-array-loop/
from typing import List
class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
visited = [False] * len(nums)
for i in range(len(nums)):
if visited[i] == False:
newVisited = [False] * len(nums)
t = i
while True:
visited[t] = True
newVisited[t] = True
t2 = (t + nums[t] + len(nums)) % len(nums)
if t == t2:
break
t = t2
if newVisited[t] == True:
symbol = True
if nums[t] > 0:
symbol = True
elif nums[t] == 0:
break
else:
symbol = False
allTrue = True
t2 = (t + nums[t]) % len(nums)
while True:
if t2 == t:
break
elif symbol:
if nums[t2] <= 0:
allTrue = False
break
else:
if nums[t2] >= 0:
allTrue = False
break
t2 = (t2 + nums[t2]) % len(nums)
if allTrue:
return True
else:
break
return False
def test_1():
solution = Solution()
print(solution.circularArrayLoop([-2,1,-1,-2,-2]))
if __name__ == "__main__":
test_1()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62