未验证 提交 daf5939d 编写于 作者: K Keqi Huang 提交者: GitHub

Update 457._Circular_Array_Loop.md

上级 48aefb34
......@@ -28,7 +28,7 @@ Can you do it in O(n) time complexity and O(1) space complexity?
> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
快慢指针,
快慢指针,然后要注意的是如果loop只有一个元素不算loop,每次找完一圈后发现没有找到的话要将这一路上经过的点全都设为0防止下次重复查找了
```python
......@@ -46,23 +46,25 @@ class Solution(object):
if val == 0:
continue
j = i
k = get_next_idx(i)
slow = i
fast = get_next_idx(i)
while nums[k] * val > 0 and nums[get_next_idx(k)] * val > 0:
if j == k:
if j == get_next_idx(j):
while nums[fast] * val > 0 and nums[get_next_idx(fast)] * val > 0:
if slow == fast:
# exclude for loop with only one element
if slow == get_next_idx(slow):
break
return True
j = get_next_idx(j)
k = get_next_idx(get_next_idx(k))
slow = get_next_idx(slow)
fast = get_next_idx(get_next_idx(fast))
j = i
while nums[j] * val > 0:
nxt = get_next_idx(j)
nums[j] = 0
j = nxt
slow = i
# set all the element along the path to 0, avoiding repeated lookup
while nums[slow] * val > 0:
nxt = get_next_idx(slow)
nums[slow] = 0
slow = nxt
return False
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册