Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
awesome-algorithm
提交
daf5939d
A
awesome-algorithm
项目概览
OpenDocCN
/
awesome-algorithm
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
awesome-algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
daf5939d
编写于
9月 19, 2018
作者:
K
Keqi Huang
提交者:
GitHub
9月 19, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update 457._Circular_Array_Loop.md
上级
48aefb34
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
15 addition
and
13 deletion
+15
-13
docs/Leetcode_Solutions/457._Circular_Array_Loop.md
docs/Leetcode_Solutions/457._Circular_Array_Loop.md
+15
-13
未找到文件。
docs/Leetcode_Solutions/457._Circular_Array_Loop.md
浏览文件 @
daf5939d
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录