Skip to content

Commit 1587800

Browse files
author
huoda
committed
Merge remote-tracking branch 'upstream/master'
2 parents e3897a0 + cae60fa commit 1587800

File tree

178 files changed

+9861
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+9861
-423
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*.tar.gz
2020
*.rar
2121
*.DS_Store
22+
*.exe
2223

2324
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2425
hs_err_pid*
@@ -29,3 +30,22 @@ hs_err_pid*
2930

3031
# WebStorm
3132
.idea/
33+
34+
# Build results
35+
[Dd]ebug/
36+
[Dd]ebugPublic/
37+
[Rr]elease/
38+
[Rr]eleases/
39+
x64/
40+
x86/
41+
bld/
42+
[Bb]in/
43+
[Oo]bj/
44+
[Ll]og/
45+
46+
# Visual Studio 2015/2017 cache/options directory
47+
.vs/
48+
49+
**/*.idea
50+
**/*.iml
51+
**/*out

DynamicStackBaseArray.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package Stack;
2+
3+
import java.util.Iterator;
4+
5+
/**
6+
* 顺序栈的动态扩容
7+
* Author: PeiJiaNi
8+
* @param <T> 顺序栈元素类型
9+
*/
10+
11+
public class DynamicStackBaseArray<T> implements Iterable<T> {
12+
private T[] items; // 数组
13+
private int count; // 栈中的元素个数
14+
private int length; // 栈空间大小
15+
16+
/**
17+
* 初始化栈
18+
*
19+
* @param length 栈空间大小
20+
*/
21+
public DynamicStackBaseArray(int length) {
22+
this.items = (T[]) new Object[length];
23+
this.count = 0;
24+
this.length = length;
25+
}
26+
27+
/**
28+
* 入栈操作 平均时间复杂度O(1)
29+
*
30+
* @param item 入栈元素
31+
*/
32+
public void push(T item) {
33+
// 栈空间已满,则扩容
34+
if (count == length) {
35+
resize(2 * items.length);
36+
}
37+
38+
items[count++] = item;
39+
}
40+
41+
/**
42+
* 出栈操作 平均时间复杂度O(1)
43+
*
44+
* @return 如果栈内不为空,则返回栈顶元素,否则返回-1
45+
*/
46+
public T pop() {
47+
if (count == 0) {
48+
System.out.println("当前栈已空,无法进行出栈操作");
49+
return null;
50+
}
51+
52+
T item = items[--count];
53+
items[count] = null;
54+
55+
if (count > 0 && (count == items.length / 4)) {
56+
resize(items.length / 2);
57+
}
58+
59+
// 返回下标为 count-1 的数组元素,并且栈中元素个数count-1
60+
return item;
61+
}
62+
63+
/**
64+
* 栈空间动态增加或减小
65+
*
66+
* @param size
67+
*/
68+
private void resize(int size) {
69+
T[] newItems = (T[]) new Object[size];
70+
for (int i = 0; i < count; i++) {
71+
newItems[i] = this.items[i];
72+
}
73+
this.items = newItems;
74+
}
75+
76+
//返回栈中最近添加的元素而不删除它
77+
public T peek() {
78+
return items[count - 1];
79+
}
80+
81+
/**
82+
* 判断当前栈是否为空
83+
*
84+
* @return 栈为空,则返回true,否则返回-1
85+
*/
86+
public boolean isEmpty() {
87+
return count == 0;
88+
}
89+
90+
/**
91+
* 返回栈中元素个数
92+
*
93+
* @return
94+
*/
95+
public int size() {
96+
return count;
97+
}
98+
99+
@Override
100+
public Iterator<T> iterator() {
101+
return new ArrayIterator();
102+
}
103+
104+
// 内部类
105+
class ArrayIterator implements Iterator {
106+
int numOfItems = count;
107+
108+
@Override
109+
public boolean hasNext() {
110+
return numOfItems > 0;
111+
}
112+
113+
@Override
114+
public T next() {
115+
return items[--numOfItems];
116+
}
117+
}
118+
119+
public static void main(String[] args) {
120+
DynamicStackBaseArray<Integer> stack = new DynamicStackBaseArray<Integer>(6);
121+
stack.push(1);
122+
stack.push(2);
123+
stack.push(3);
124+
stack.push(4);
125+
stack.push(5);
126+
// System.out.println(stack.peek());
127+
Iterator iterator = stack.iterator();
128+
// System.out.println(iterator.hasNext());
129+
while (iterator.hasNext()) {
130+
System.out.println(iterator.next());
131+
}
132+
133+
}
134+
135+
}
136+

README.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
1-
# 数据结构和算法之美
2-
# [https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)
1+
# 数据结构和算法必知必会的50个代码实现
2+
### 微信搜索我的公众号“小争哥”,或者微信扫描下面二维码, 获取更多压箱底的干货分享
3+
### 前Google工程师,5万+人跟着学的《数据结构和算法之美》专栏作者
4+
![t2](https://github.com/wangzheng0822/markdownphotos/blob/master/pics/qrcode_for_gh_9b0e7afdff20_258.jpg)
35

4-
# Java rate limiting library/framework
5-
# https://github.com/wangzheng0822/ratelimiter4j
6+
## 数组
7+
* 实现一个支持动态扩容的数组
8+
* 实现一个大小固定的有序数组,支持动态增删改操作
9+
* 实现两个有序数组合并为一个有序数组
10+
11+
## 链表
12+
* 实现单链表、循环链表、双向链表,支持增删操作
13+
* 实现单链表反转
14+
* 实现两个有序的链表合并为一个有序链表
15+
* 实现求链表的中间结点
16+
17+
##
18+
* 用数组实现一个顺序栈
19+
* 用链表实现一个链式栈
20+
* 编程模拟实现一个浏览器的前进、后退功能
21+
22+
## 队列
23+
* 用数组实现一个顺序队列
24+
* 用链表实现一个链式队列
25+
* 实现一个循环队列
26+
27+
## 递归
28+
* 编程实现斐波那契数列求值f(n)=f(n-1)+f(n-2)
29+
* 编程实现求阶乘n!
30+
* 编程实现一组数据集合的全排列
31+
32+
## 排序
33+
* 实现归并排序、快速排序、插入排序、冒泡排序、选择排序
34+
* 编程实现O(n)时间复杂度内找到一组数据的第K大元素
35+
36+
## 二分查找
37+
* 实现一个有序数组的二分查找算法
38+
* 实现模糊二分查找算法(比如大于等于给定值的第一个元素)
39+
40+
## 散列表
41+
* 实现一个基于链表法解决冲突问题的散列表
42+
* 实现一个LRU缓存淘汰算法
43+
44+
## 字符串
45+
* 实现一个字符集,只包含a~z这26个英文字母的Trie树
46+
* 实现朴素的字符串匹配算法
47+
48+
## 二叉树
49+
* 实现一个二叉查找树,并且支持插入、删除、查找操作
50+
* 实现查找二叉查找树中某个节点的后继、前驱节点
51+
* 实现二叉树前、中、后序以及按层遍历
52+
53+
##
54+
* 实现一个小顶堆、大顶堆、优先级队列
55+
* 实现堆排序
56+
* 利用优先级队列合并K个有序数组
57+
* 求一组动态数据集合的最大Top K
58+
59+
##
60+
* 实现有向图、无向图、有权图、无权图的邻接矩阵和邻接表表示方法
61+
* 实现图的深度优先搜索、广度优先搜索
62+
* 实现Dijkstra算法、A*算法
63+
* 实现拓扑排序的Kahn算法、DFS算法
64+
65+
## 回溯
66+
* 利用回溯算法求解八皇后问题
67+
* 利用回溯算法求解0-1背包问题
68+
69+
## 分治
70+
* 利用分治算法求一组数据的逆序对个数
71+
72+
## 动态规划
73+
* 0-1背包问题
74+
* 最小路径和
75+
* 编程实现莱文斯坦最短编辑距离
76+
* 编程实现查找两个字符串的最长公共子序列
77+
* 编程实现一个数据序列的最长递增子序列

StackBaseArray.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package Stack;
2+
3+
/**
4+
* 顺序栈(基于数组实现)
5+
* Author: PeiJiaNi
6+
*/
7+
public class StackBaseArray {
8+
private int[] items; // 数组
9+
private int count; // 栈中元素个数
10+
private int length; // 栈空间大小
11+
12+
public StackBaseArray(int capactiy) {
13+
this.items = new int[capactiy];
14+
this.count = 0;
15+
this.length = capactiy;
16+
}
17+
18+
/**
19+
* 入栈操作 时间复杂度O(1)
20+
* @param item 要入栈的元素
21+
* @return 入栈成功则返回true,否则返回false
22+
*/
23+
public boolean push(int item) {
24+
if(count == length) {
25+
System.out.println("当前栈已满,无法进行入栈操作");
26+
return false;
27+
}
28+
items[count] = item;
29+
++count;
30+
return true;
31+
}
32+
33+
/**
34+
* 出栈操作 时间复杂度O(1)
35+
* @return 如果栈内不为空,则返回栈顶元素,否则返回-1
36+
*/
37+
public int pop(){
38+
if(count == 0) {
39+
System.out.println("当前栈已空,无法进行出栈操作");
40+
return -1;
41+
}
42+
43+
// 返回下标为 count-1 的数组元素,并且栈中元素个数count-1
44+
return items[--count];
45+
}
46+
47+
public static void main(String[] args){
48+
StackBaseArray stack = new StackBaseArray(6);
49+
stack.push(1);
50+
stack.push(2);
51+
stack.push(3);
52+
stack.push(4);
53+
stack.push(5);
54+
System.out.println(stack.pop());
55+
System.out.println(stack.pop());
56+
System.out.println(stack.pop());
57+
System.out.println(stack.pop());
58+
System.out.println(stack.pop());
59+
60+
}
61+
62+
63+
}
64+

c-cpp/05_array/array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int delete(struct array *array, int idx)
4747
return -1;
4848

4949
memmove(&array->arr[idx], &array->arr[idx+1],
50-
(array->used - idx) * sizeof(int));
50+
(array->used - idx - 1) * sizeof(int));
5151
array->used--;
5252
return 0;
5353
}

0 commit comments

Comments
 (0)