Skip to content

Commit 657b0dd

Browse files
committed
Created book structure
1 parent fadddb7 commit 657b0dd

File tree

12 files changed

+456
-2
lines changed

12 files changed

+456
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.grunt
3+
/_book/

LANGS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
* [Java](java)
2+
* [C++](cpp)
3+
* [C#](cs)
4+
* [Python](py)
5+
* [Ruby](rb)
6+
* [JavaScript](js)
7+
* [Swift](swift)
8+
* [Scala](scala)
9+
* [Go](go)
10+
* [Clojure](clj)

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
# algorithm-essentials
2-
算法精粹--举一反三,触类旁通,抛弃题海战术
1+
# 算法精粹——举一反三,抛弃题海战术
2+
3+
市场上讲解算法的书已经汗牛充栋,我为什么还要写这本书呢?主要原因是我对目前市场上的大部分算法书都不太满意。 本书有如下特色:
4+
5+
1. 每道题都有在线OJ支持。本书的所有题目,都可以在 [www.algohub.org](http://www.algohub.org) 上找到。这样的一大好处是,读者可以边看书,边实现自己的代码,然后提交到网站上验证自己的想法是否正确。
6+
2. 每道题都有完整的代码。市场上的大部分书,都会讲思路,但给出的代码都是片段,不是完整可编译的代码。本书每题都有完整的代码,且每个代码经过千锤百炼,保证可读性的前提下尽可能简短,方面读者在面试中能快速写出来。
7+
3. 每道题都有多种解法。本书的宗旨是,用尽可能少的题目,覆盖尽可能多的算法。本书中的的每道题都有多种解法,每种解法不是简单的小改进,而是完全不同的思路,力求举一反三,让读者触类旁通。
8+
4. 本书支持多种主流编程语言。目前支持 Java, C++, C#, Python, Ruby, JavaScript, Swift, Scala, Clojure, 将来还会支持更多编程语言。
9+
10+
11+
# 多种编程语言
12+
13+
* [Java](java)
14+
* [C++](cpp)
15+
* [C#](cs)
16+
* [Python](py)
17+
* [Ruby](rb)
18+
* [JavaScript](js)
19+
* [Swift](swift)
20+
* [Scala](scala)
21+
* [Go](go)
22+
* [Clojure](clj)
23+
24+
# Community
25+
26+
QQ 群: 237669375
27+
28+
## Acknowledgments
29+
30+
## License
31+
Book License: [CC BY-SA 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/)

book.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"gitbook": "2.x.x",
3+
"plugins": ["mathjax"],
4+
"links": {
5+
"sidebar": {
6+
"Contact us / Support": "https://www.github.com/soulmachine"
7+
}
8+
},
9+
"pluginsConfig": {}
10+
}

cpp/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Summary
2+
3+
* [第一部分 基础算法](basic-part.md)
4+
* [线性表](linear-list/README.md)
5+
* [数组](linear-list/array.md)
6+
* [链表](linear-list/linked-list.md)
7+
* [字符串](string/README.md)
8+
* [栈和队列](stack-and-queue/README.md)
9+
* [栈](stack-and-queue/stack.md)
10+
* [队列](stack-and-queue/queue.md)
11+
* [二叉树](binary-tree/README.md)
12+
* [二叉树的遍历](binary-tree/traversal.md)
13+
* [二叉树的构建](binary-tree/construction.md)
14+
* [二叉查找树](binary-tree/bst.md)
15+
* [二叉树的递归](binary-tree/recursion.md)
16+
* [排序](sort/README.md)
17+
* [查找](search/README.md)
18+
* [暴力枚举法](brute-force/README.md)
19+
* [深度优先搜索](dfs/README.md)
20+
* [广度优先搜索](bfs/README.md)
21+
* [分治法](divide-and-conquer/README.md)
22+
* [贪心法](greedy/README.md)
23+
* [动态规划](dp/README.md)
24+
* [第一部分 高级算法](advanced-part.md)
25+
* [](graph/README.md)
26+

cpp/SUMMARY.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Summary
2+
3+
* [第一部分 基础算法](basic-part.md)
4+
* [线性表](linear-list/README.md)
5+
* [数组](linear-list/array.md)
6+
* [链表](linear-list/linked-list.md)
7+
* [字符串](string/README.md)
8+
* [栈和队列](stack-and-queue/README.md)
9+
* [栈](stack-and-queue/stack.md)
10+
* [队列](stack-and-queue/queue.md)
11+
* [二叉树](binary-tree/README.md)
12+
* [二叉树的遍历](binary-tree/traversal.md)
13+
* [二叉树的构建](binary-tree/construction.md)
14+
* [二叉查找树](binary-tree/bst.md)
15+
* [二叉树的递归](binary-tree/recursion.md)
16+
* [排序](sort/README.md)
17+
* [查找](search/README.md)
18+
* [暴力枚举法](brute-force/README.md)
19+
* [深度优先搜索](dfs/README.md)
20+
* [广度优先搜索](bfs/README.md)
21+
* [分治法](divide-and-conquer/README.md)
22+
* [贪心法](greedy/README.md)
23+
* [动态规划](dp/README.md)
24+
* [第一部分 高级算法](advanced-part.md)
25+
* [](graph/README.md)
26+

cpp/linear-list/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
这类题目考察线性表的操作,例如,数组,单链表,双向链表等。

cpp/linear-list/array.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# 数组
2+
3+
## Remove Duplicates from Sorted Array
4+
5+
### 描述
6+
7+
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
8+
9+
Do not allocate extra space for another array, you must do this in place with constant memory.
10+
11+
For example, Given input array `A = [1,1,2]`,
12+
13+
Your function should return length = 2, and `A` is now `[1,2]`.
14+
15+
16+
### 分析
17+
18+
19+
20+
21+
### 代码1
22+
23+
```cpp
24+
// LeetCode, Remove Duplicates from Sorted Array
25+
// 时间复杂度O(n),空间复杂度O(1)
26+
class Solution {
27+
public:
28+
int removeDuplicates(vector<int>& nums) {
29+
if (nums.empty()) return 0;
30+
31+
int index = 0;
32+
for (int i = 1; i < nums.size(); i++) {
33+
if (nums[index] != nums[i])
34+
nums[++index] = nums[i];
35+
}
36+
return index + 1;
37+
}
38+
};
39+
```
40+
41+
42+
### 代码2
43+
44+
```cpp
45+
// 使用STL,时间复杂度O(n),空间复杂度O(1)
46+
class Solution {
47+
public:
48+
int removeDuplicates(vector<int>& nums) {
49+
return distance(nums.begin(), unique(nums.begin(), nums.end()));
50+
}
51+
};
52+
```
53+
54+
55+
### 代码3
56+
57+
```cpp
58+
// 使用STL,时间复杂度O(n),空间复杂度O(1)
59+
class Solution {
60+
public:
61+
int removeDuplicates(vector<int>& nums) {
62+
return distance(nums.begin(), removeDuplicates(nums.begin(), nums.end(), nums.begin()));
63+
}
64+
65+
template<typename InIt, typename OutIt>
66+
OutIt removeDuplicates(InIt first, InIt last, OutIt output) {
67+
while (first != last) {
68+
*output++ = *first;
69+
first = upper_bound(first, last, *first);
70+
}
71+
72+
return output;
73+
}
74+
};
75+
```
76+
77+
78+
### 相关题目
79+
80+
* [Remove Duplicates from Sorted Array II](#remove-duplicates-from-sorted-array-ii)
81+
82+
83+
## Remove Duplicates from Sorted Array II
84+
85+
### 描述
86+
87+
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?
88+
89+
For example, given sorted array `A = [1,1,1,2,2,3]`, your function should return length = `5`, and A is now `[1,1,2,2,3]`
90+
91+
92+
### 分析
93+
94+
加一个变量记录一下元素出现的次数即可。这题因为是已经排序的数组,所以一个变量即可解决。如果是没有排序的数组,则需要引入一个hashmap来记录出现次数。
95+
96+
97+
### 代码1
98+
99+
```cpp
100+
// LeetCode, Remove Duplicates from Sorted Array II
101+
// 时间复杂度O(n),空间复杂度O(1)
102+
// @author hex108 (https://github.com/hex108)
103+
class Solution {
104+
public:
105+
int removeDuplicates(vector<int>& nums) {
106+
if (nums.size() <= 2) return nums.size();
107+
108+
int index = 2;
109+
for (int i = 2; i < nums.size(); i++){
110+
if (nums[i] != nums[index - 2])
111+
nums[index++] = nums[i];
112+
}
113+
114+
return index;
115+
}
116+
};
117+
```
118+
119+
120+
### 代码2
121+
122+
下面是一个更简洁的版本。上面的代码略长,不过扩展性好一些,例如将\fn{occur < 2}改为\fn{occur < 3},就变成了允许重复最多3次。
123+
124+
```cpp
125+
// LeetCode, Remove Duplicates from Sorted Array II
126+
// @author 虞航仲 (http://weibo.com/u/1666779725)
127+
// 时间复杂度O(n),空间复杂度O(1)
128+
class Solution {
129+
public:
130+
int removeDuplicates(vector<int>& nums) {
131+
const int n = nums.size();
132+
int index = 0;
133+
for (int i = 0; i < n; ++i) {
134+
if (i > 0 && i < n - 1 && nums[i] == nums[i - 1] && nums[i] == nums[i + 1])
135+
continue;
136+
137+
nums[index++] = nums[i];
138+
}
139+
return index;
140+
}
141+
};
142+
```
143+
144+
145+
### 相关题目
146+
147+
* [Remove Duplicates from Sorted Array](#remove-duplicates-from-sorted-array)
148+

java/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Summary
2+
3+
* [第一部分 基础算法](basic-part.md)
4+
* [线性表](linear-list/README.md)
5+
* [数组](linear-list/array.md)
6+
* [链表](linear-list/linked-list.md)
7+
* [字符串](string/README.md)
8+
* [栈和队列](stack-and-queue/README.md)
9+
* [栈](stack-and-queue/stack.md)
10+
* [队列](stack-and-queue/queue.md)
11+
* [二叉树](binary-tree/README.md)
12+
* [二叉树的遍历](binary-tree/traversal.md)
13+
* [二叉树的构建](binary-tree/construction.md)
14+
* [二叉查找树](binary-tree/bst.md)
15+
* [二叉树的递归](binary-tree/recursion.md)
16+
* [排序](sort/README.md)
17+
* [查找](search/README.md)
18+
* [暴力枚举法](brute-force/README.md)
19+
* [深度优先搜索](dfs/README.md)
20+
* [广度优先搜索](bfs/README.md)
21+
* [分治法](divide-and-conquer/README.md)
22+
* [贪心法](greedy/README.md)
23+
* [动态规划](dp/README.md)
24+
* [第一部分 高级算法](advanced-part.md)
25+
* [](graph/README.md)
26+

java/SUMMARY.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Summary
2+
3+
* [第一部分 基础算法](basic-part.md)
4+
* [线性表](linear-list/README.md)
5+
* [数组](linear-list/array.md)
6+
* [链表](linear-list/linked-list.md)
7+
* [字符串](string/README.md)
8+
* [栈和队列](stack-and-queue/README.md)
9+
* [栈](stack-and-queue/stack.md)
10+
* [队列](stack-and-queue/queue.md)
11+
* [二叉树](binary-tree/README.md)
12+
* [二叉树的遍历](binary-tree/traversal.md)
13+
* [二叉树的构建](binary-tree/construction.md)
14+
* [二叉查找树](binary-tree/bst.md)
15+
* [二叉树的递归](binary-tree/recursion.md)
16+
* [排序](sort/README.md)
17+
* [查找](search/README.md)
18+
* [暴力枚举法](brute-force/README.md)
19+
* [深度优先搜索](dfs/README.md)
20+
* [广度优先搜索](bfs/README.md)
21+
* [分治法](divide-and-conquer/README.md)
22+
* [贪心法](greedy/README.md)
23+
* [动态规划](dp/README.md)
24+
* [第一部分 高级算法](advanced-part.md)
25+
* [](graph/README.md)
26+

0 commit comments

Comments
 (0)