Skip to content

Commit 31d6f02

Browse files
committed
update
1 parent f5856ce commit 31d6f02

File tree

11 files changed

+530
-18
lines changed

11 files changed

+530
-18
lines changed

README.md

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
# Algorithm Ttutorial
1+
# algorithm-tutorial
22

3+
> 算法、数据结构这类知识点真的需要天分。本人不是算法工程师,也做不了算法工程师。总是在见识到一些算法大神的优雅实现后,感叹于自己是多么的弱智,同样是写代码,我写的是什么垃圾!
4+
>
5+
> 这个项目的初衷,是将工作中经常会接触到的一些数据结构和算法整理归纳。人总是要有理想的,要想少写垃圾的代码,必须夯实算法的基础。路漫漫兮其修远,吾将上下而求索。
6+
>
37
> 🔁 项目同步维护在 [github](https://github.com/dunwu/algorithm-tutorial) | [gitee](https://gitee.com/turnon/algorithm-tutorial)
48
>
59
> 📖 [电子书](https://dunwu.github.io/algorithm-tutorial/) | [电子书(国内)](http://turnon.gitee.io/algorithm-tutorial/)
610
7-
## 笔记
11+
## 原理
12+
13+
### 数据结构
14+
15+
- 线性表
16+
- [数组](docs/data-structure/array.md)
17+
- [](docs/data-structure/stack.md)
18+
- [队列](docs/data-structure/queue.md)
19+
- [链表](docs/data-structure/list.md)
20+
- [](docs/data-structure/tree/README.md)
21+
- [二叉树](docs/data-structure/tree/binary-tree.md)
22+
- [红黑树](docs/data-structure/tree/red-black-tree.md)
23+
- B 树
24+
- B+ 树
25+
- [](docs/data-structure/graph.md)
26+
- [](docs/data-structure/heap.md)
27+
- [散列表](docs/data-structure/hash.md)
28+
29+
### 算法
830

9-
- [查找](docs/search/README.md)
10-
- [线性表的查找](docs/search/linear-list-search.md)
11-
- [哈希表的查找](docs/search/hash-search.md)
12-
- [排序](docs/sort/README.md)
13-
- [冒泡排序](docs/sort/bubble-sort.md)
14-
- [快速排序](docs/sort/quick-sort.md)
15-
- [直接插入排序](docs/sort/insert-sort.md)
16-
- [希尔排序](docs/sort/shell-sort.md)
17-
- [简单选择排序](docs/sort/selection-sort.md)
18-
- [堆排序](docs/sort/heap-sort.md)
19-
- [归并排序](docs/sort/merge-sort.md)
20-
- [基数排序](docs/sort/radix-sort.md)
31+
- [排序](docs/algorithm/sort.md)
32+
- [线性表的查找](docs/algorithm/linear-list-search.md)
33+
- [哈希表的查找](docs/algorithm/hash-search.md)
2134

22-
## 源码
35+
## 刷题
2336

2437
### 数组
2538

@@ -47,3 +60,51 @@
4760
- [反转字符串](https://github.com/dunwu/algorithm/blob/master/codes/data-structure/src/main/java/io/github/dunwu/ds/str/ReverseString.java)
4861
- [反转字符串中的单词](https://github.com/dunwu/algorithm/blob/master/codes/data-structure/src/main/java/io/github/dunwu/ds/str/ReverseWordsInAString.java)
4962
- [反转字符串中的单词 III ](https://github.com/dunwu/algorithm/blob/master/codes/data-structure/src/main/java/io/github/dunwu/ds/str/ReverseWordsInAString3.java)
63+
64+
## 📚 学习资源
65+
66+
- **书籍**
67+
- 刷题必备
68+
- 《剑指 offer》
69+
- 《编程之美》
70+
- 《编程之法:面试和算法心得》
71+
- 《算法谜题》 都是思维题
72+
- 基础
73+
-[编程珠玑(第 2 版)](https://www.amazon.cn/gp/product/B00SFZH0DC/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B00SFZH0DC&linkCode=as2&tag=vastwork-23)
74+
-[编程珠玑(续)](https://www.amazon.cn/gp/product/B0150BMQDM/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B0150BMQDM&linkCode=as2&tag=vastwork-23)
75+
-[数据结构与算法分析 : C++描述(第 4 版)](https://www.amazon.cn/gp/product/B01LDG2DSG/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B01LDG2DSG&linkCode=as2&tag=vastwork-23)
76+
-[数据结构与算法分析 : C 语言描述(第 2 版)](https://www.amazon.cn/gp/product/B002WC7NGS/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B002WC7NGS&linkCode=as2&tag=vastwork-23)
77+
-[数据结构与算法分析 : Java 语言描述(第 2 版)](https://www.amazon.cn/gp/product/B01CNP0CG6/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B01CNP0CG6&linkCode=as2&tag=vastwork-23)
78+
-[算法(第 4 版)](https://www.amazon.cn/gp/product/B009OCFQ0O/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B009OCFQ0O&linkCode=as2&tag=vastwork-23)
79+
- 算法设计
80+
-[算法设计与分析基础(第 3 版)](https://www.amazon.cn/gp/product/B00S4HCQUI/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B00S4HCQUI&linkCode=as2&tag=vastwork-23)
81+
- 《Algorithm Design Manual》 - 算法设计手册 红皮书
82+
- [《算法导论》](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) - 是一本对算法介绍比较全面的经典书籍
83+
- 《Algorithms on Strings,Trees and Sequences》
84+
- 《Advanced Data Structures》 - 各种诡异高级的数据结构和算法 如元胞自动机、斐波纳契堆、线段树 600 块
85+
- 参考链接和学习网站
86+
- https://github.com/nonstriater/Learn-Algorithms
87+
- https://github.com/trekhleb/javascript-algorithms
88+
- https://github.com/kdn251/interviews/blob/master/README-zh-cn.md#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84
89+
- [July 博客](http://blog.csdn.net/v_july_v)
90+
- 《数学建模十大经典算法》
91+
- 《数据挖掘领域十大经典算法》
92+
- 《十道海量数据处理面试题》
93+
- 《数字图像处理领域的二十四个经典算法》
94+
- 《精选微软等公司经典的算法面试 100 题》
95+
- [The-Art-Of-Programming-By-July](https://github.com/julycoding/The-Art-Of-Programming-By-July)
96+
- [微软面试 100 题](http://blog.csdn.net/column/details/ms100.html)
97+
- [程序员编程艺术](http://blog.csdn.net/v_JULY_v/article/details/6460494)
98+
- 基本算法演示
99+
- <http://sjjg.js.zwu.edu.cn/SFXX/sf1/sfys.html>
100+
- <http://www.cs.usfca.edu/\~galles/visualization/Algorithms.html>
101+
- 编程网站
102+
- [leetcode](http://leetcode.com/)
103+
- [openjudge](http://openjudge.cn/)
104+
- 其它
105+
- [高级数据结构和算法](https://www.coursera.org/learn/gaoji-shuju-jiegou/) 北大教授张铭老师在 coursera 上的课程。完成这门课之时,你将掌握多维数组、广义表、Trie 树、AVL 树、伸展树等高级数据结构,并结合内排序、外排序、检索、索引有关的算法,高效地解决现实生活中一些比较复杂的应用问题。当然 coursera 上也还有很多其它算法方面的视频课程。
106+
- [算法设计与分析 Design and Analysis of Algorithms](https://class.coursera.org/algorithms-001/lecture) 由北大教授 Wanling Qu 在 coursera 讲授的一门算法课程。首先介绍一些与算法有关的基础知识,然后阐述经典的算法设计思想和分析技术,主要涉及的算法设计技术是:分治策略、动态规划、贪心法、回溯与分支限界等。每个视频都配有相应的讲义(pdf 文件)以便阅读和复习。
107+
108+
## 🚪 传送门
109+
110+
| [技术文档归档](https://github.com/dunwu/blog) | [算法和数据结构教程系列](https://github.com/dunwu/algorithm-tutorial) |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.dunwu.algorithm.array;
2+
3+
/**
4+
* 给定一个包含了一些 0 和 1的非空二维数组 grid ,
5+
* <p>
6+
* 一个岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
7+
* <p>
8+
* 找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
9+
* <p>
10+
* 示例 1:
11+
* <p>
12+
* [[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], [0,1,0,0,1,1,0,0,1,0,1,0,0],
13+
* [0,1,0,0,1,1,0,0,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0]]
14+
* 对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
15+
* <p>
16+
* 示例 2:
17+
* <p>
18+
* [[0,0,0,0,0,0,0,0]] 对于上面这个给定的矩阵, 返回 0。
19+
* <p>
20+
* 注意: 给定的矩阵grid 的长度和宽度都不超过 50。
21+
*
22+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
23+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/243/array-and-sorting/1034/">岛屿的最大面积</a>
24+
* @since 2020-01-18
25+
*/
26+
public class MaxAreaOfIsland {
27+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package io.github.dunwu.ds.list;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
8+
* @since 2020-01-18
9+
*/
10+
public class LeetcodeListDemo {
11+
12+
/**
13+
* <code>合并两个有序链表</code> 算法实现
14+
*
15+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/244/linked-list-and-tree/1048/">合并两个有序链表</a>
16+
*/
17+
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
18+
ListNode root = new ListNode(-1);
19+
20+
ListNode node = root;
21+
while (l1 != null && l2 != null) {
22+
if (l1.val <= l2.val) {
23+
node.next = l1;
24+
l1 = l1.next;
25+
} else {
26+
node.next = l2;
27+
l2 = l2.next;
28+
}
29+
node = node.next;
30+
}
31+
32+
if (l1 == null) {
33+
node.next = l2;
34+
} else {
35+
node.next = l1;
36+
}
37+
38+
return root.next;
39+
}
40+
41+
/**
42+
* <code>合并K个排序链表</code> 算法实现
43+
* <p>
44+
* 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
45+
* <p>
46+
* 示例:
47+
* <p>
48+
* 输入:
49+
*
50+
* <pre>
51+
* [
52+
* 1->4->5,
53+
* 1->3->4,
54+
* 2->6
55+
* ]
56+
* </pre>
57+
* <p>
58+
* 输出: 1->1->2->3->4->4->5->6
59+
*
60+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/244/linked-list-and-tree/1025/">合并K个排序链表</a>
61+
*/
62+
public static ListNode mergeKLists(ListNode[] lists) {
63+
if (lists == null || lists.length == 0) {
64+
return null;
65+
}
66+
67+
ListNode root = new ListNode(-1);
68+
ListNode resultHead = root;
69+
while (true) {
70+
Integer minIndex = null;
71+
Integer minVal = null;
72+
for (int i = 0; i < lists.length; i++) {
73+
if (lists[i] == null) {
74+
continue;
75+
}
76+
77+
if (minVal == null || lists[i].val < minVal) {
78+
minIndex = i;
79+
minVal = lists[i].val;
80+
}
81+
}
82+
83+
if (minIndex != null) {
84+
resultHead.next = new ListNode(lists[minIndex].val);
85+
resultHead = resultHead.next;
86+
lists[minIndex] = lists[minIndex].next;
87+
} else {
88+
break;
89+
}
90+
}
91+
92+
return root.next;
93+
}
94+
95+
public static ListNode mergeKLists2(ListNode[] lists) {
96+
if (lists == null || lists.length == 1) {
97+
return lists[0];
98+
}
99+
100+
ListNode result = lists[0];
101+
for (int i = 1; i < lists.length; i++) {
102+
result = mergeTwoLists(result, lists[i]);
103+
}
104+
return result;
105+
}
106+
107+
/**
108+
* <code>反转链表</code> 算法实现
109+
*
110+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/244/linked-list-and-tree/1038/">反转链表</a>
111+
*/
112+
public static ListNode reverseList(ListNode head) {
113+
if (head == null) {
114+
return null;
115+
}
116+
117+
ListNode prev = null;
118+
ListNode curr = head;
119+
while (curr != null) {
120+
ListNode temp = curr.next;
121+
curr.next = prev;
122+
prev = curr;
123+
curr = temp;
124+
}
125+
return prev;
126+
}
127+
128+
/**
129+
* 给出两个 非空 的链表用来表示两个非负的整数。
130+
* <p>
131+
* 其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
132+
* <p>
133+
* 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
134+
* <p>
135+
* 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
136+
* <p>
137+
* 示例:
138+
* <p>
139+
* 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
140+
* <p>
141+
* 输出:7 -> 0 -> 8
142+
* <p>
143+
* 原因:342 + 465 = 807
144+
*
145+
* <code>两数相加</code> 算法实现
146+
*
147+
* @see <a href="https://leetcode-cn.com/explore/featured/card/bytedance/244/linked-list-and-tree/1022/">两数相加</a>
148+
*/
149+
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
150+
151+
// 如果任意一个表示数的链表为空,直接返回另一个链表
152+
if (l1 == null) return l2;
153+
if (l2 == null) return l1;
154+
155+
// 初始化
156+
int carry = 0;
157+
ListNode x = l1;
158+
ListNode y = l2;
159+
ListNode resultHead = new ListNode(-1);
160+
ListNode currNode = resultHead;
161+
162+
// 同时遍历两个操作数链表,任意操作数链表的当前位数所对应元素不为 null 则累加
163+
while (x != null || y != null) {
164+
int value = carry;
165+
166+
if (x != null) {
167+
value += x.val;
168+
x = x.next;
169+
}
170+
171+
if (y != null) {
172+
value += y.val;
173+
y = y.next;
174+
}
175+
176+
carry = value / 10;
177+
currNode.next = new ListNode(value % 10);
178+
currNode = currNode.next;
179+
}
180+
181+
if (carry != 0) {
182+
currNode.next = new ListNode(carry);
183+
}
184+
return resultHead.next;
185+
}
186+
187+
public static List<Integer> getValues(ListNode listNode) {
188+
List<Integer> list = new ArrayList<>();
189+
ListNode item = listNode;
190+
while (item != null) {
191+
list.add(item.val);
192+
item = item.next;
193+
}
194+
return list;
195+
}
196+
197+
public static void printList(ListNode result) {
198+
while (result != null) {
199+
System.out.print(result.val + " ");
200+
result = result.next;
201+
}
202+
System.out.println();
203+
}
204+
205+
public static class ListNode {
206+
207+
int val;
208+
209+
ListNode next;
210+
211+
ListNode(int val) { this.val = val; }
212+
213+
}
214+
215+
}

0 commit comments

Comments
 (0)