Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
06190f2f
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
06190f2f
编写于
8月 28, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
03bb846a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
57 addition
and
21 deletion
+57
-21
notes/算法.md
notes/算法.md
+57
-21
未找到文件。
notes/算法.md
浏览文件 @
06190f2f
...
...
@@ -104,17 +104,21 @@ public class ThreeSumSlow implements ThreeSum {
public
int
count
(
int
[]
nums
)
{
int
N
=
nums
.
length
;
int
cnt
=
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
for
(
int
j
=
i
+
1
;
j
<
N
;
j
++)
for
(
int
k
=
j
+
1
;
k
<
N
;
k
++)
if
(
nums
[
i
]
+
nums
[
j
]
+
nums
[
k
]
==
0
)
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
for
(
int
j
=
i
+
1
;
j
<
N
;
j
++)
{
for
(
int
k
=
j
+
1
;
k
<
N
;
k
++)
{
if
(
nums
[
i
]
+
nums
[
j
]
+
nums
[
k
]
==
0
)
{
cnt
++;
}
}
}
}
return
cnt
;
}
}
```
### 2. ThreeSum
Fast
### 2. ThreeSum
BinarySearch
通过将数组先排序,对两个元素求和,并用二分查找方法查找是否存在该和的相反数,如果存在,就说明存在三元组的和为 0。
...
...
@@ -123,19 +127,23 @@ public class ThreeSumSlow implements ThreeSum {
该方法可以将 ThreeSum 算法增长数量级降低为 O(N
<sup>
2
</sup>
logN)。
```
java
public
class
ThreeSumFast
{
public
static
int
count
(
int
[]
nums
)
{
public
class
ThreeSumBinarySearch
implements
ThreeSum
{
@Override
public
int
count
(
int
[]
nums
)
{
Arrays
.
sort
(
nums
);
int
N
=
nums
.
length
;
int
cnt
=
0
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
for
(
int
j
=
i
+
1
;
j
<
N
;
j
++)
{
int
target
=
-
nums
[
i
]
-
nums
[
j
];
int
index
=
BinarySearch
.
search
(
nums
,
target
);
// 应该注意这里的下标必须大于 j,否则会重复统计。
if
(
index
>
j
)
if
(
index
>
j
)
{
cnt
++;
}
}
}
return
cnt
;
}
}
...
...
@@ -143,22 +151,59 @@ public class ThreeSumFast {
```
java
public
class
BinarySearch
{
public
static
int
search
(
int
[]
nums
,
int
target
)
{
int
l
=
0
,
h
=
nums
.
length
-
1
;
while
(
l
<=
h
)
{
int
m
=
l
+
(
h
-
l
)
/
2
;
if
(
target
==
nums
[
m
])
if
(
target
==
nums
[
m
])
{
return
m
;
else
if
(
target
>
nums
[
m
])
}
else
if
(
target
>
nums
[
m
])
{
l
=
m
+
1
;
else
}
else
{
h
=
m
-
1
;
}
}
return
-
1
;
}
}
```
### 3. ThreeSumTwoPointer
更有效的方法是先将数组排序,然后使用双指针进行查找,时间复杂度为 O(N
<sup>
2
</sup>
)。
```
java
public
class
ThreeSumTwoPointer
implements
ThreeSum
{
@Override
public
int
count
(
int
[]
nums
)
{
int
N
=
nums
.
length
;
int
cnt
=
0
;
Arrays
.
sort
(
nums
);
for
(
int
i
=
0
;
i
<
N
-
2
;
i
++)
{
int
l
=
i
+
1
,
h
=
N
-
1
,
target
=
-
nums
[
i
];
if
(
i
>
0
&&
nums
[
i
]
==
nums
[
i
-
1
])
continue
;
while
(
l
<
h
)
{
int
sum
=
nums
[
l
]
+
nums
[
h
];
if
(
sum
==
target
)
{
cnt
++;
while
(
l
<
h
&&
nums
[
l
]
==
nums
[
l
+
1
])
l
++;
while
(
l
<
h
&&
nums
[
h
]
==
nums
[
h
-
1
])
h
--;
l
++;
h
--;
}
else
if
(
sum
<
target
)
{
l
++;
}
else
{
h
--;
}
}
}
return
cnt
;
}
}
```
## 倍率实验
如果 T(N)
\~
aN
<sup>
b
</sup>
logN,那么 T(2N)/T(N)
\~
2
<sup>
b
</sup>
。
...
...
@@ -180,29 +225,20 @@ public class BinarySearch {
public
class
RatioTest
{
public
static
void
main
(
String
[]
args
)
{
int
N
=
500
;
int
loopTimes
=
7
;
double
preTime
=
-
1
;
while
(
loopTimes
--
>
0
)
{
int
[]
nums
=
new
int
[
N
];
StopWatch
.
start
();
ThreeSum
threeSum
=
new
ThreeSumSlow
();
int
cnt
=
threeSum
.
count
(
nums
);
System
.
out
.
println
(
cnt
);
double
elapsedTime
=
StopWatch
.
elapsedTime
();
double
ratio
=
preTime
==
-
1
?
0
:
elapsedTime
/
preTime
;
System
.
out
.
println
(
N
+
" "
+
elapsedTime
+
" "
+
ratio
);
preTime
=
elapsedTime
;
N
*=
2
;
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录