Skip to content

Commit 8ac4e34

Browse files
authored
Merge pull request #1 from doocs/master
merge
2 parents 184af5a + ebda970 commit 8ac4e34

File tree

281 files changed

+9862
-3215
lines changed

Some content is hidden

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

281 files changed

+9862
-3215
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<p align="center">
66
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,C++,Python,JavaScript,Go,.../green?list=1" alt="languages"></a>
77
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
8-
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
9-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
8+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
109
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
1110
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1211
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
@@ -18,25 +17,24 @@
1817
[English Version](/README_EN.md)
1918

2019
## 站点
20+
- Netlify: https://lc.netlify.app
2121
- GitHub Pages: https://doocs.github.io/leetcode
2222
- Coding Pages: https://d3jc40.coding-pages.com
2323

24-
注:若 [GitHub Pages](https://doocs.github.io/leetcode) 访问加载过慢,可访问 [Coding Pages](https://d3jc40.coding-pages.com)
25-
2624
## 题解
2725
- [LeetCode](/solution/README.md)
28-
- [剑指 Offer(第 2 版)](/lcof/README.md)
29-
- [程序员面试金典(第 6 版)](/lcci/README.md)
26+
- [LeetCode 《剑指 Offer(第 2 版)](/lcof/README.md)
27+
- [LeetCode 《程序员面试金典(第 6 版)](/lcci/README.md)
3028

3129
## 维护者
3230
[Yang Libin](https://github.com/yanglbme): GitHub 技术社区 [@Doocs](https://github.com/doocs) 创建者;[@TheAlgorithms](https://github.com/TheAlgorithms) 组织成员。
3331

3432
## 加入我们
35-
刷编程题的最大好处就是**可以锻炼解决问题的思维能力**。相信我,「如何去思考」​本身也是一项需要不断学习和练习的技能。非常感谢前微软工程师、现蚂蚁金服技术专家 [@kfstorm](https://github.com/kfstorm) 贡献了本项目的所有 [C# 题解](https://github.com/doocs/leetcode/pull/245)
33+
刷编程题的最大好处就是可以锻炼解决问题的思维能力。相信我,「如何去思考」​本身也是一项需要不断学习和练习的技能。非常感谢前微软工程师、现蚂蚁金服技术专家 [@kfstorm](https://github.com/kfstorm) 贡献了本项目的所有 [C# 题解](https://github.com/doocs/leetcode/pull/245)
3634

3735
如果你对本项目感兴趣,并且希望加入我们刷题大军,欢迎随时提交 [PR](https://github.com/doocs/leetcode/pulls)。请参考如下步骤:
3836

39-
-[本项目](https://github.com/doocs/leetcode) <kbd>Fork</kbd> 到你的个人 GitHub 帐户,然后 <kbd>clone</kbd> 到你的本地机器;
37+
-[本项目](https://github.com/doocs/leetcode) <kbd>fork</kbd> 到你的个人 GitHub 帐户,然后 <kbd>clone</kbd> 到你的本地机器;
4038
- 对项目做出一些变更,然后使用 git <kbd>add</kbd>、<kbd>commit</kbd>、<kbd>push</kbd> 等命令将你的变更提交到你的远程 GitHub 仓库;
4139
- 将你的变更以 PR 的形式提交过来,项目的维护人员会在第一时间对你的变更进行 review!
4240
- 你也可以参考 [GitHub 帮助文档](https://help.github.com/cn) 了解更多细节。

README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<p align="center">
66
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,C++,Python,JavaScript,Go,.../green?list=1" alt="languages"></a>
77
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
8-
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
9-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
8+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
109
<a href="https://github.com/doocs/leetcode/blob/master/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
1110
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
1211
<a href="https://doocs.github.io/#/?id=how-to-join"><img src="https://badgen.net/badge/organization/join%20us/cyan" alt="open-source-organization"></a>
@@ -18,6 +17,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
1817
[中文文档](/README.md)
1918

2019
## Sites
20+
- Netlify: https://lc.netlify.app
2121
- GitHub Pages: https://doocs.github.io/leetcode
2222
- Coding Pages: https://d3jc40.coding-pages.com
2323

basic/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 算法
2+
3+
## 常见的排序算法
4+
5+
- 冒泡排序
6+
- 插入排序
7+
- 归并排序

basic/sort/BubbleSort.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
public class BubbleSort {
4+
private static void swap(int[] nums, int i, int j) {
5+
int t = nums[i];
6+
nums[i] = nums[j];
7+
nums[j] = t;
8+
}
9+
10+
private static void bubbleSort(int[] nums) {
11+
boolean hasChange = true;
12+
for (int i = 0; i < nums.length - 1 && hasChange; ++i) {
13+
hasChange = false;
14+
for (int j = 0; j < nums.length - 1 - i; ++j) {
15+
if (nums[j] > nums[j + 1]) {
16+
swap(nums, j, j + 1);
17+
hasChange = true;
18+
}
19+
}
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] nums = {1, 2, 7, 9, 5, 8};
25+
bubbleSort(nums);
26+
System.out.println(Arrays.toString(nums));
27+
}
28+
}

basic/sort/InsertionSort.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
3+
public class InsertionSort {
4+
private static void insertionSort(int[] nums) {
5+
for (int i = 1, j, current; i < nums.length; ++i) {
6+
current = nums[i];
7+
for (j = i - 1; j >= 0 && nums[j] > current; --j) {
8+
nums[j + 1] = nums[j];
9+
}
10+
nums[j + 1] = current;
11+
}
12+
}
13+
14+
public static void main(String[] args) {
15+
int[] nums = { 1, 2, 7, 9, 5, 8 };
16+
insertionSort(nums);
17+
System.out.println(Arrays.toString(nums));
18+
}
19+
}

basic/sort/MergeSort.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Arrays;
2+
3+
public class MergeSort {
4+
5+
private static void merge(int[] nums, int low, int mid, int high, int[] temp) {
6+
int i = low, j = mid + 1, k = low;
7+
while (k <= high) {
8+
if (i > mid) {
9+
temp[k++] = nums[j++];
10+
} else if (j > high) {
11+
temp[k++] = nums[i++];
12+
} else if (nums[i] <= nums[j]) {
13+
temp[k++] = nums[i++];
14+
} else {
15+
temp[k++] = nums[j++];
16+
}
17+
}
18+
19+
for (i = low; i <= high; ++i) {
20+
nums[i] = temp[i];
21+
}
22+
}
23+
24+
private static void mergeSort(int[] nums, int low, int high, int[] temp) {
25+
if (low >= high) {
26+
return;
27+
}
28+
int mid = low + ((high - low) >> 1);
29+
mergeSort(nums, low, mid, temp);
30+
mergeSort(nums, mid + 1, high, temp);
31+
merge(nums, low, mid, high, temp);
32+
}
33+
34+
private static void mergeSort(int[] nums) {
35+
int[] temp = new int[nums.length];
36+
mergeSort(nums, 0, nums.length - 1, temp);
37+
}
38+
39+
public static void main(String[] args) {
40+
int[] nums = {1, 2, 7, 4, 5, 3};
41+
mergeSort(nums);
42+
System.out.println(Arrays.toString(nums));
43+
}
44+
}

basic/sort/README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# 常见的排序算法
2+
3+
## 冒泡排序
4+
5+
定义一个布尔变量 `hasChange`,用来标记每轮是否进行了交换。在每轮遍历开始时,将 `hasChange` 设置为 false。
6+
7+
### 代码示例
8+
9+
```java
10+
import java.util.Arrays;
11+
12+
public class BubbleSort {
13+
private static void swap(int[] nums, int i, int j) {
14+
int t = nums[i];
15+
nums[i] = nums[j];
16+
nums[j] = t;
17+
}
18+
19+
private static void bubbleSort(int[] nums) {
20+
boolean hasChange = true;
21+
for (int i = 0; i < nums.length - 1 && hasChange; ++i) {
22+
hasChange = false;
23+
for (int j = 0; j < nums.length - 1 - i; ++j) {
24+
if (nums[j] > nums[j + 1]) {
25+
swap(nums, j, j + 1);
26+
hasChange = true;
27+
}
28+
}
29+
}
30+
}
31+
32+
public static void main(String[] args) {
33+
int[] nums = {1, 2, 7, 9, 5, 8};
34+
bubbleSort(nums);
35+
System.out.println(Arrays.toString(nums));
36+
}
37+
}
38+
```
39+
40+
### 算法分析
41+
42+
空间复杂度 O(1)、时间复杂度 O(n^2)。
43+
44+
分情况讨论:
45+
46+
1. 给定的数组按照顺序已经排好:只需要进行 `n-1` 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
47+
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。
48+
3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 O(n^2)。
49+
50+
因此,时间复杂度是 O(n^2),这是一种稳定的排序算法。
51+
52+
> 稳定是指,两个相等的数,在排序过后,相对位置保持不变。
53+
54+
## 插入排序
55+
56+
与冒泡排序对比:
57+
58+
- 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。
59+
- 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的。
60+
61+
插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。
62+
63+
### 代码示例
64+
65+
```java
66+
import java.util.Arrays;
67+
68+
public class InsertionSort {
69+
private static void insertionSort(int[] nums) {
70+
for (int i = 1, j, current; i < nums.length; ++i) {
71+
current = nums[i];
72+
for (j = i - 1; j >= 0 && nums[j] > current; --j) {
73+
nums[j + 1] = nums[j];
74+
}
75+
nums[j + 1] = current;
76+
}
77+
}
78+
79+
public static void main(String[] args) {
80+
int[] nums = { 1, 2, 7, 9, 5, 8 };
81+
insertionSort(nums);
82+
System.out.println(Arrays.toString(nums));
83+
}
84+
}
85+
```
86+
87+
### 算法分析
88+
89+
空间复杂度 O(1),时间复杂度 O(n^2)。
90+
91+
分情况讨论:
92+
93+
1. 给定的数组按照顺序排好序:只需要进行 n-1 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
94+
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。
95+
3. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n^2)。
96+
97+
因此,时间复杂度是 O(n^2),这也是一种稳定的排序算法。
98+
99+
## 归并排序
100+
101+
归并排序的核心思想是分治,把一个复杂问题拆分成若干个子问题来求解。
102+
103+
归并排序的算法思想是:把数组从中间划分为两个子数组,一直递归地把子数组划分成更小的数组,直到子数组里面只有一个元素的时候开始排序。排序的方法就是按照大小顺序合并两个元素。接着依次按照递归的顺序返回,不断合并排好序的数组,直到把整个数组排好序。
104+
105+
### 代码示例
106+
107+
```java
108+
import java.util.Arrays;
109+
110+
public class MergeSort {
111+
112+
private static void merge(int[] nums, int low, int mid, int high, int[] temp) {
113+
int i = low, j = mid + 1, k = low;
114+
while (k <= high) {
115+
if (i > mid) {
116+
temp[k++] = nums[j++];
117+
} else if (j > high) {
118+
temp[k++] = nums[i++];
119+
} else if (nums[i] <= nums[j]) {
120+
temp[k++] = nums[i++];
121+
} else {
122+
temp[k++] = nums[j++];
123+
}
124+
}
125+
126+
for (i = low; i <= high; ++i) {
127+
nums[i] = temp[i];
128+
}
129+
}
130+
131+
private static void mergeSort(int[] nums, int low, int high, int[] temp) {
132+
if (low >= high) {
133+
return;
134+
}
135+
int mid = low + ((high - low) >> 1);
136+
mergeSort(nums, low, mid, temp);
137+
mergeSort(nums, mid + 1, high, temp);
138+
merge(nums, low, mid, high, temp);
139+
}
140+
141+
private static void mergeSort(int[] nums) {
142+
int n = nums.length;
143+
int[] temp = new int[n];
144+
mergeSort(nums, 0, n - 1, temp);
145+
}
146+
147+
public static void main(String[] args) {
148+
int[] nums = {1, 2, 7, 4, 5, 3};
149+
mergeSort(nums);
150+
System.out.println(Arrays.toString(nums));
151+
}
152+
}
153+
```
154+
155+
### 算法分析
156+
157+
空间复杂度 O(n),时间复杂度 O(nlogn)。
158+
159+
对于规模为 n 的问题,一共要进行 log(n) 次的切分,每一层的合并复杂度都是 O(n),所以整体时间复杂度为 O(nlogn)。
160+
161+
由于合并 n 个元素需要分配一个大小为 n 的额外数组,所以空间复杂度为 O(n)。
162+
163+
这是一种稳定的排序算法。
164+
165+
## 快速排序
166+
167+
快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。

index.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,27 @@
4141
}
4242
},
4343
plugins: [
44-
function (hook) {
45-
var footer = [
46-
'<hr/>',
47-
'<footer>',
48-
'<span>Copyright © 2018-2020 <a href="https://github.com/doocs" target="_blank">Doocs</a>. All rights reserved.',
49-
'</footer>'
50-
].join('')
51-
52-
hook.afterEach(function (html) {
53-
return html + footer
54-
})
55-
},
5644
function (hook, vm) {
5745
hook.beforeEach(function (html) {
46+
const en = vm.route.file.indexOf('README_EN') > -1
5847
if (/githubusercontent\.com/.test(vm.route.file)) {
5948
url = vm.route.file
6049
.replace('raw.githubusercontent.com', 'github.com')
6150
.replace(/\/master/, '/blob/master')
6251
} else {
6352
url = 'https://github.com/doocs/leetcode/blob/master/' + vm.route.file
6453
}
65-
var editHtml = '[:memo: Edit on GitHub](' + url + ')\n'
54+
const edit = en ? 'Edit on GitHub' : '在 GitHub 编辑'
55+
const editHtml = `[:memo: ${edit}](${url})\n`
6656
return editHtml + html
6757
})
58+
59+
hook.afterEach(function (html) {
60+
const en = vm.route.file.indexOf('README_EN') != -1
61+
const copyright = en ? '. All Rights Reserved' : ' 版权所有'
62+
const footer = `<footer><span>Copyright © 2018-2020 <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`
63+
return html + footer
64+
})
6865
},
6966
]
7067
}

0 commit comments

Comments
 (0)