Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
b5f3fe01
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,发现更多精彩内容 >>
提交
b5f3fe01
编写于
4月 29, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
775d62a4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
62 addition
and
27 deletion
+62
-27
notes/Leetcode 题解.md
notes/Leetcode 题解.md
+62
-27
pics/3b49dd67-2c40-4b81-8ad2-7bbb1fe2fcbd.png
pics/3b49dd67-2c40-4b81-8ad2-7bbb1fe2fcbd.png
+0
-0
未找到文件。
notes/Leetcode 题解.md
浏览文件 @
b5f3fe01
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
*
[
快速选择
](
#快速选择
)
*
[
快速选择
](
#快速选择
)
*
[
堆排序
](
#堆排序
)
*
[
堆排序
](
#堆排序
)
*
[
桶排序
](
#桶排序
)
*
[
桶排序
](
#桶排序
)
*
[
荷兰国旗问题
](
#荷兰国旗问题
)
*
[
搜索
](
#搜索
)
*
[
搜索
](
#搜索
)
*
[
BFS
](
#bfs
)
*
[
BFS
](
#bfs
)
*
[
DFS
](
#dfs
)
*
[
DFS
](
#dfs
)
...
@@ -855,9 +856,8 @@ public int findKthLargest(int[] nums, int k) {
...
@@ -855,9 +856,8 @@ public int findKthLargest(int[] nums, int k) {
PriorityQueue
<
Integer
>
pq
=
new
PriorityQueue
<>();
// 小顶堆
PriorityQueue
<
Integer
>
pq
=
new
PriorityQueue
<>();
// 小顶堆
for
(
int
val
:
nums
)
{
for
(
int
val
:
nums
)
{
pq
.
add
(
val
);
pq
.
add
(
val
);
if
(
pq
.
size
()
>
k
)
{
if
(
pq
.
size
()
>
k
)
// 维护堆的大小为 K
pq
.
poll
();
pq
.
poll
();
}
}
}
return
pq
.
peek
();
return
pq
.
peek
();
}
}
...
@@ -871,9 +871,12 @@ public int findKthLargest(int[] nums, int k) {
...
@@ -871,9 +871,12 @@ public int findKthLargest(int[] nums, int k) {
int
l
=
0
,
h
=
nums
.
length
-
1
;
int
l
=
0
,
h
=
nums
.
length
-
1
;
while
(
l
<
h
)
{
while
(
l
<
h
)
{
int
j
=
partition
(
nums
,
l
,
h
);
int
j
=
partition
(
nums
,
l
,
h
);
if
(
j
==
k
)
break
;
if
(
j
==
k
)
if
(
j
<
k
)
l
=
j
+
1
;
break
;
else
h
=
j
-
1
;
else
if
(
j
<
k
)
l
=
j
+
1
;
else
h
=
j
-
1
;
}
}
return
nums
[
k
];
return
nums
[
k
];
}
}
...
@@ -883,7 +886,8 @@ private int partition(int[] a, int l, int h) {
...
@@ -883,7 +886,8 @@ private int partition(int[] a, int l, int h) {
while
(
true
)
{
while
(
true
)
{
while
(
a
[++
i
]
<
a
[
l
]
&&
i
<
h
)
;
while
(
a
[++
i
]
<
a
[
l
]
&&
i
<
h
)
;
while
(
a
[--
j
]
>
a
[
l
]
&&
j
>
l
)
;
while
(
a
[--
j
]
>
a
[
l
]
&&
j
>
l
)
;
if
(
i
>=
j
)
break
;
if
(
i
>=
j
)
break
;
swap
(
a
,
i
,
j
);
swap
(
a
,
i
,
j
);
}
}
swap
(
a
,
l
,
j
);
swap
(
a
,
l
,
j
);
...
@@ -891,9 +895,9 @@ private int partition(int[] a, int l, int h) {
...
@@ -891,9 +895,9 @@ private int partition(int[] a, int l, int h) {
}
}
private
void
swap
(
int
[]
a
,
int
i
,
int
j
)
{
private
void
swap
(
int
[]
a
,
int
i
,
int
j
)
{
int
t
mp
=
a
[
i
];
int
t
=
a
[
i
];
a
[
i
]
=
a
[
j
];
a
[
i
]
=
a
[
j
];
a
[
j
]
=
t
mp
;
a
[
j
]
=
t
;
}
}
```
```
...
@@ -912,24 +916,22 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2].
...
@@ -912,24 +916,22 @@ Given [1,1,1,2,2,3] and k = 2, return [1,2].
```
java
```
java
public
List
<
Integer
>
topKFrequent
(
int
[]
nums
,
int
k
)
{
public
List
<
Integer
>
topKFrequent
(
int
[]
nums
,
int
k
)
{
Map
<
Integer
,
Integer
>
frequencyMap
=
new
HashMap
<>();
Map
<
Integer
,
Integer
>
frequencyMap
=
new
HashMap
<>();
for
(
int
num
:
nums
)
{
for
(
int
num
:
nums
)
frequencyMap
.
put
(
num
,
frequencyMap
.
getOrDefault
(
num
,
0
)
+
1
);
frequencyMap
.
put
(
num
,
frequencyMap
.
getOrDefault
(
num
,
0
)
+
1
);
}
List
<
Integer
>[]
bucket
=
new
List
[
nums
.
length
+
1
];
List
<
Integer
>[]
bucket
=
new
List
[
nums
.
length
+
1
];
for
(
int
key
:
frequencyMap
.
keySet
())
{
for
(
int
key
:
frequencyMap
.
keySet
())
{
int
frequency
=
frequencyMap
.
get
(
key
);
int
frequency
=
frequencyMap
.
get
(
key
);
if
(
bucket
[
frequency
]
==
null
)
{
if
(
bucket
[
frequency
]
==
null
)
bucket
[
frequency
]
=
new
ArrayList
<>();
bucket
[
frequency
]
=
new
ArrayList
<>();
}
bucket
[
frequency
].
add
(
key
);
bucket
[
frequency
].
add
(
key
);
}
}
List
<
Integer
>
topK
=
new
ArrayList
<>();
List
<
Integer
>
topK
=
new
ArrayList
<>();
for
(
int
i
=
bucket
.
length
-
1
;
i
>=
0
&&
topK
.
size
()
<
k
;
i
--)
{
for
(
int
i
=
bucket
.
length
-
1
;
i
>=
0
&&
topK
.
size
()
<
k
;
i
--)
if
(
bucket
[
i
]
!=
null
)
{
if
(
bucket
[
i
]
!=
null
)
topK
.
addAll
(
bucket
[
i
]);
topK
.
addAll
(
bucket
[
i
]);
}
}
return
topK
;
return
topK
;
}
}
```
```
...
@@ -953,32 +955,65 @@ So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid ans
...
@@ -953,32 +955,65 @@ So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid ans
```
java
```
java
public
String
frequencySort
(
String
s
)
{
public
String
frequencySort
(
String
s
)
{
Map
<
Character
,
Integer
>
frequencyMap
=
new
HashMap
<>();
Map
<
Character
,
Integer
>
frequencyMap
=
new
HashMap
<>();
for
(
char
c
:
s
.
toCharArray
())
{
for
(
char
c
:
s
.
toCharArray
())
frequencyMap
.
put
(
c
,
frequencyMap
.
getOrDefault
(
c
,
0
)
+
1
);
frequencyMap
.
put
(
c
,
frequencyMap
.
getOrDefault
(
c
,
0
)
+
1
);
}
List
<
Character
>[]
frequencyBucket
=
new
List
[
s
.
length
()
+
1
];
List
<
Character
>[]
frequencyBucket
=
new
List
[
s
.
length
()
+
1
];
for
(
char
c
:
frequencyMap
.
keySet
())
{
for
(
char
c
:
frequencyMap
.
keySet
())
{
int
f
=
frequencyMap
.
get
(
c
);
int
f
=
frequencyMap
.
get
(
c
);
if
(
frequencyBucket
[
f
]
==
null
)
{
if
(
frequencyBucket
[
f
]
==
null
)
frequencyBucket
[
f
]
=
new
ArrayList
<>();
frequencyBucket
[
f
]
=
new
ArrayList
<>();
}
frequencyBucket
[
f
].
add
(
c
);
frequencyBucket
[
f
].
add
(
c
);
}
}
StringBuilder
str
=
new
StringBuilder
();
StringBuilder
str
=
new
StringBuilder
();
for
(
int
frequency
=
frequencyBucket
.
length
-
1
;
frequency
>=
0
;
frequency
--)
{
for
(
int
i
=
frequencyBucket
.
length
-
1
;
i
>=
0
;
i
--)
{
if
(
frequencyBucket
[
frequency
]
==
null
)
{
if
(
frequencyBucket
[
i
]
==
null
)
continue
;
continue
;
}
for
(
char
c
:
frequencyBucket
[
i
])
for
(
char
c
:
frequencyBucket
[
frequency
])
{
for
(
int
j
=
0
;
j
<
i
;
j
++)
for
(
int
i
=
0
;
i
<
frequency
;
i
++)
{
str
.
append
(
c
);
str
.
append
(
c
);
}
}
}
}
return
str
.
toString
();
return
str
.
toString
();
}
}
```
```
### 荷兰国旗问题
荷兰国旗包含三种颜色:红、白、蓝。有这三种颜色的球,算法的目标是将这三种球按颜色顺序正确地排列。
它其实是三向切分快速排序的一种变种,在三向切分快速排序中,每次切分都将数组分成三个区间:小于切分元素、等于切分元素、大于切分元素,而该算法是将数组分成三个区间:等于红色、等于白色、等于蓝色。
<div
align=
"center"
>
<img
src=
"../pics//3b49dd67-2c40-4b81-8ad2-7bbb1fe2fcbd.png"
/>
</div><br>
**对颜色进行排序**
[
75. Sort Colors (Medium)
](
https://leetcode.com/problems/sort-colors/description/
)
```
html
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
```
```
java
public
void
sortColors
(
int
[]
nums
)
{
int
zero
=
-
1
,
one
=
0
,
two
=
nums
.
length
;
while
(
one
<
two
)
{
if
(
nums
[
one
]
==
0
)
swap
(
nums
,
++
zero
,
one
++);
else
if
(
nums
[
one
]
==
2
)
swap
(
nums
,
--
two
,
one
);
else
++
one
;
}
}
private
void
swap
(
int
[]
nums
,
int
i
,
int
j
)
{
int
t
=
nums
[
i
];
nums
[
i
]
=
nums
[
j
];
nums
[
j
]
=
t
;
}
```
## 搜索
## 搜索
深度优先搜索和广度优先搜索广泛运用于树和图中,但是它们的应用远远不止如此。
深度优先搜索和广度优先搜索广泛运用于树和图中,但是它们的应用远远不止如此。
...
...
pics/3b49dd67-2c40-4b81-8ad2-7bbb1fe2fcbd.png
0 → 100644
浏览文件 @
b5f3fe01
1.0 KB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录