Skip to content

Commit ae07862

Browse files
committed
update codes and docs
1 parent 24df9d6 commit ae07862

28 files changed

+1830
-1023
lines changed

README.md

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
# algorithm-tutorial
1+
<p align="center">
2+
<a href="https://dunwu.github.io/algorithm-tutorial/" target="_blank" rel="noopener noreferrer">
3+
<img src="http://dunwu.test.upcdn.net/common/logo/dunwu-logo.png" alt="logo" width="150px"/>
4+
</a>
5+
</p>
6+
7+
<p align="center">
8+
<img src="https://badgen.net/github/license/dunwu/algorithm-tutorial" alt="license">
9+
<img src="https://travis-ci.com/dunwu/algorithm-tutorial.svg?branch=master" alt="build">
10+
</p>
11+
12+
<h1 align="center">algorithm-tutorial</h1>
213

314
> 算法、数据结构这类知识点真的需要天分。本人不是算法工程师,也做不了算法工程师。总是在见识到一些算法大神的优雅实现后,感叹于自己是多么的弱智,同样是写代码,我写的是什么垃圾!
415
>
@@ -10,74 +21,58 @@
1021
1122
## 内容
1223

13-
[数组](docs/array2.md)
14-
15-
[链表](docs/list.md)
16-
17-
[](docs/stack.md)
18-
19-
[队列](docs/queue.md)
20-
21-
递归
22-
23-
[排序](docs/sort.md)
24-
25-
查找
26-
27-
跳表
28-
29-
[散列表](docs/hash.md)
30-
31-
[](docs/tree/README.md)
32-
33-
[二叉树](docs/tree/binary-tree.md)
34-
35-
[红黑树](docs/tree/red-black-tree.md)
36-
37-
[](docs/graph.md)
38-
39-
[](docs/heap.md)
40-
41-
[字典树](docs/trie.md)
42-
43-
字符串
44-
45-
贪心算法
46-
47-
分治算法
48-
49-
回溯算法
50-
51-
动态规划
24+
- [数组](docs/array.md)
25+
- [链表](docs/list.md)
26+
- [](docs/stack.md)
27+
- [队列](docs/queue.md)
28+
- 递归
29+
- [排序](docs/sort.md)
30+
- 查找
31+
- 跳表
32+
- [散列表](docs/hash.md)
33+
- [](docs/tree/README.md)
34+
- [二叉树](docs/tree/binary-tree.md)
35+
- [红黑树](docs/tree/red-black-tree.md)
36+
- [](docs/graph.md)
37+
- [](docs/heap.md)
38+
- [字典树](docs/trie.md)
39+
- 字符串
40+
- 贪心算法
41+
- 分治算法
42+
- 回溯算法
43+
- 动态规划
5244

5345
## 刷题
5446

5547
### 数组
5648

57-
- 三数之和
58-
- 两数之和
59-
- 二维数组
60-
- 删除排序数组中的重复项
61-
- 加一
62-
- 在排序数组中查找元素的第一个和最后一个位置
63-
- 在排序数组中查找数字 I
64-
- 存在重复元素
65-
- 对角线遍历
66-
- 寻找数组的中心索引
67-
- 将数组分成和相等的三个部分
68-
- 数组二分查找
69-
- 数组拆分 1
70-
- 旋转数组
71-
- 旋转矩阵
72-
- 最大连续 1 的个数
73-
- 杨辉三角
74-
- 杨辉三角 2
75-
- 移动零
76-
- 移除元素
77-
- 至少是其他数字两倍的最大数
78-
- 螺旋矩阵
79-
- 长度最小的子数组
80-
- 零矩阵
49+
- [ArrayDemo.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/ArrayDemo.java)
50+
- [三数之和.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/三数之和.java)
51+
- [两数之和.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/两数之和.java)
52+
- [二维数组.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/二维数组.java)
53+
- [删除排序数组中的重复项.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/删除排序数组中的重复项.java)
54+
- [加一.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/加一.java)
55+
- [在排序数组中查找元素的第一个和最后一个位置.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/在排序数组中查找元素的第一个和最后一个位置.java)
56+
- [在排序数组中查找数字 I.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/在排序数组中查找数字I.java)
57+
- [存在重复元素.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/存在重复元素.java)
58+
- [对角线遍历.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/对角线遍历.java)
59+
- [寻找数组的中心索引.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/寻找数组的中心索引.java)
60+
- [将数组分成和相等的三个部分.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/将数组分成和相等的三个部分.java)
61+
- [数组二分查找.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/数组二分查找.java)
62+
- [数组拆分 1.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/数组拆分1.java)
63+
- [旋转数组.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/旋转数组.java)
64+
- [旋转矩阵.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/旋转矩阵.java)
65+
- [最大连续 1 的个数.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/最大连续1的个数.java)
66+
- [杨辉三角.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/杨辉三角.java)
67+
- [杨辉三角 2.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/杨辉三角2.java)
68+
- [模拟 ArrayList1.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/模拟ArrayList1.java)
69+
- [模拟 ArrayList2.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/模拟ArrayList2.java)
70+
- [移动零.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/移动零.java)
71+
- [移除元素.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/移除元素.java)
72+
- [至少是其他数字两倍的最大数.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/至少是其他数字两倍的最大数.java)
73+
- [螺旋矩阵.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/螺旋矩阵.java)
74+
- [长度最小的子数组.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/长度最小的子数组.java)
75+
- [零矩阵.java](https://github.com/dunwu/algorithm-tutorial/blob/master/codes/algorithm/src/main/java/io/github/dunwu/algorithm/array/零矩阵.java)
8176

8277
### 字符串
8378

@@ -88,7 +83,7 @@
8883
- [反转字符串中的单词](https://github.com/dunwu/algorithm/blob/master/codes/data-structure/src/main/java/io/github/dunwu/ds/str/ReverseWordsInAString.java)
8984
- [反转字符串中的单词 III ](https://github.com/dunwu/algorithm/blob/master/codes/data-structure/src/main/java/io/github/dunwu/ds/str/ReverseWordsInAString3.java)
9085

91-
## 📚 学习资源
86+
## 📚 资料
9287

9388
- **书籍**
9489
- 刷题必备
@@ -109,10 +104,11 @@
109104
- [《算法导论》](https://www.amazon.cn/gp/product/B00AK7BYJY/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B00AK7BYJY&linkCode=as2&tag=vastwork-23) - 是一本对算法介绍比较全面的经典书籍
110105
- 《Algorithms on Strings,Trees and Sequences》
111106
- 《Advanced Data Structures》 - 各种诡异高级的数据结构和算法 如元胞自动机、斐波纳契堆、线段树 600 块
112-
- 参考链接和学习网站
107+
- **学习网站**
113108
- https://github.com/TheAlgorithms/Java
114109
- https://github.com/nonstriater/Learn-Algorithms
115110
- https://github.com/trekhleb/javascript-algorithms
111+
- https://github.com/wangzheng0822/algo
116112
- https://github.com/kdn251/interviews/blob/master/README-zh-cn.md#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84
117113
- [July 博客](http://blog.csdn.net/v_july_v)
118114
- 《数学建模十大经典算法》
@@ -133,6 +129,6 @@
133129
- [高级数据结构和算法](https://www.coursera.org/learn/gaoji-shuju-jiegou/) 北大教授张铭老师在 coursera 上的课程。完成这门课之时,你将掌握多维数组、广义表、Trie 树、AVL 树、伸展树等高级数据结构,并结合内排序、外排序、检索、索引有关的算法,高效地解决现实生活中一些比较复杂的应用问题。当然 coursera 上也还有很多其它算法方面的视频课程。
134130
- [算法设计与分析 Design and Analysis of Algorithms](https://class.coursera.org/algorithms-001/lecture) 由北大教授 Wanling Qu 在 coursera 讲授的一门算法课程。首先介绍一些与算法有关的基础知识,然后阐述经典的算法设计思想和分析技术,主要涉及的算法设计技术是:分治策略、动态规划、贪心法、回溯与分支限界等。每个视频都配有相应的讲义(pdf 文件)以便阅读和复习。
135131

136-
## 🚪 传送门
132+
## 🚪 传送
137133

138134
| [技术文档归档](https://github.com/dunwu/blog) | [算法和数据结构教程系列](https://github.com/dunwu/algorithm-tutorial) |
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package io.github.dunwu.algorithm.list;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 基于单链表LRU算法(java)
7+
*
8+
* @author hoda
9+
* @create 2018-12-17
10+
*/
11+
public class LRUBaseLinkedList<T> {
12+
13+
/**
14+
* 默认链表容量
15+
*/
16+
private final static Integer DEFAULT_CAPACITY = 10;
17+
18+
/**
19+
* 头结点
20+
*/
21+
private Node<T> headNode;
22+
23+
/**
24+
* 链表长度
25+
*/
26+
private Integer length;
27+
28+
/**
29+
* 链表容量
30+
*/
31+
private Integer capacity;
32+
33+
public LRUBaseLinkedList() {
34+
this.headNode = new Node<>();
35+
this.capacity = DEFAULT_CAPACITY;
36+
this.length = 0;
37+
}
38+
39+
public LRUBaseLinkedList(Integer capacity) {
40+
this.headNode = new Node<>();
41+
this.capacity = capacity;
42+
this.length = 0;
43+
}
44+
45+
public void add(T data) {
46+
Node<T> preNode = findPreNode(data);
47+
48+
// 链表中存在,删除原数据,再插入到链表的头部
49+
if (preNode != null) {
50+
deleteElemOptim(preNode);
51+
intsertElemAtBegin(data);
52+
} else {
53+
if (length >= this.capacity) {
54+
//删除尾结点
55+
deleteElemAtEnd();
56+
}
57+
intsertElemAtBegin(data);
58+
}
59+
}
60+
61+
/**
62+
* 删除preNode结点下一个元素
63+
*
64+
* @param preNode
65+
*/
66+
private void deleteElemOptim(Node<T> preNode) {
67+
Node<T> temp = preNode.getNext();
68+
preNode.setNext(temp.getNext());
69+
temp = null;
70+
length--;
71+
}
72+
73+
/**
74+
* 链表头部插入节点
75+
*
76+
* @param data
77+
*/
78+
private void intsertElemAtBegin(T data) {
79+
Node<T> next = headNode.getNext();
80+
headNode.setNext(new Node<>(data, next));
81+
length++;
82+
}
83+
84+
/**
85+
* 获取查找到元素的前一个结点
86+
*
87+
* @param data
88+
* @return
89+
*/
90+
private Node<T> findPreNode(T data) {
91+
Node<T> node = headNode;
92+
while (node.getNext() != null) {
93+
if (data.equals(node.getNext().getElement())) {
94+
return node;
95+
}
96+
node = node.getNext();
97+
}
98+
return null;
99+
}
100+
101+
/**
102+
* 删除尾结点
103+
*/
104+
private void deleteElemAtEnd() {
105+
Node<T> ptr = headNode;
106+
// 空链表直接返回
107+
if (ptr.getNext() == null) {
108+
return;
109+
}
110+
111+
// 倒数第二个结点
112+
while (ptr.getNext().getNext() != null) {
113+
ptr = ptr.getNext();
114+
}
115+
116+
Node<T> tmp = ptr.getNext();
117+
ptr.setNext(null);
118+
tmp = null;
119+
length--;
120+
}
121+
122+
private void printAll() {
123+
Node<T> node = headNode.getNext();
124+
while (node != null) {
125+
System.out.print(node.getElement() + ",");
126+
node = node.getNext();
127+
}
128+
System.out.println();
129+
}
130+
131+
public static class Node<T> {
132+
133+
private T element;
134+
135+
private Node<T> next;
136+
137+
public Node(T element) {
138+
this.element = element;
139+
}
140+
141+
public Node(T element, Node<T> next) {
142+
this.element = element;
143+
this.next = next;
144+
}
145+
146+
public Node() {
147+
this.next = null;
148+
}
149+
150+
public T getElement() {
151+
return element;
152+
}
153+
154+
public void setElement(T element) {
155+
this.element = element;
156+
}
157+
158+
public Node<T> getNext() {
159+
return next;
160+
}
161+
162+
public void setNext(Node<T> next) {
163+
this.next = next;
164+
}
165+
166+
}
167+
168+
public static void main(String[] args) {
169+
LRUBaseLinkedList<Object> list = new LRUBaseLinkedList<>();
170+
Scanner sc = new Scanner(System.in);
171+
while (true) {
172+
list.add(sc.nextInt());
173+
list.printAll();
174+
}
175+
}
176+
177+
}

0 commit comments

Comments
 (0)