Skip to content

merge #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e49b738
feat: add python and java solutions to lcci question: 08.09.Bracket
yanglbme Apr 8, 2020
b17b2f4
feat: add python and java solutions to lcof question
yanglbme Apr 9, 2020
c9038d0
feat: add python and java solutions to lcof question
yanglbme Apr 9, 2020
42e4dbb
322. Coin Change
Gapur Apr 9, 2020
d7906c3
Merge branch 'master' of https://github.com/Gapur/leetcode
Gapur Apr 9, 2020
73cc6c6
322. Coin Change
Gapur Apr 9, 2020
97948e9
remove old code
Gapur Apr 9, 2020
899d911
Add params
poyea Apr 9, 2020
38e3e94
a
poyea Apr 9, 2020
31c0526
Merge pull request #253 from Gapur/master
yanglbme Apr 10, 2020
a573f54
fix: update leetcode solution: 0322.Coin Change
yanglbme Apr 10, 2020
f74af76
feat: update README.md
yanglbme Apr 10, 2020
ff61ae2
feat: add netlify site for leetcode project
yanglbme Apr 10, 2020
318888c
feat: update lcci questions
yanglbme Apr 10, 2020
be31a57
add Nth Digit
zhujinming Apr 10, 2020
14a4f8e
feat: add python and java solutions to lcof question
yanglbme Apr 11, 2020
f3b3830
feat: add python and java solutions to lcof question
yanglbme Apr 11, 2020
dca9082
fix: resolve type error
yanglbme Apr 11, 2020
fe9e273
feat: add python and java solutions to lcci question
yanglbme Apr 11, 2020
5f6ca51
add Nth Digit
zhujinming Apr 11, 2020
c7e85e5
feat: add python and java solutions to lcof question
yanglbme Apr 12, 2020
f0818df
feat: add python and java solutions to lcof question
yanglbme Apr 12, 2020
00f821a
feat: add solutions to lcci question: 17.04.Missing Number
yanglbme Apr 12, 2020
de8c027
feat: add python and java solutions to lcof question
yanglbme Apr 12, 2020
0fea67e
update
zhujinming Apr 12, 2020
e16f1e3
feat: add python and java solutions to lcci question: 16.01.Swap Numbers
yanglbme Apr 13, 2020
ccc4a2e
格式化代码
Apr 13, 2020
6dfdec3
格式化代码
Apr 13, 2020
c4b4e87
Merge pull request #254 from chenxuyan/master
yanglbme Apr 13, 2020
f9bf911
feat: update solutions to lcof questions
yanglbme Apr 14, 2020
29569dd
Merge pull request #255 from HendSame/master
acbin Apr 15, 2020
9e1cc06
feat: add python and java solutions to lcof question
yanglbme Apr 17, 2020
2cc8917
feat: add python and java solutions to lcci question: 17.19.Missing Two
yanglbme Apr 18, 2020
b854efc
Merge pull request #256 from HendSame/master
yanglbme Apr 18, 2020
90c2646
feat: add python and java solutions to lcci question: 17.20.Continuou…
yanglbme Apr 21, 2020
dfc2247
Merge branch 'master' of github.com:doocs/leetcode
yanglbme Apr 21, 2020
49a3e8d
feat: add basic knowledge
yanglbme Apr 26, 2020
29a5561
Merge branch 'master' of github.com:doocs/leetcode
yanglbme Apr 26, 2020
68df26c
feat: add MergeSort
yanglbme Apr 27, 2020
82a3f00
feat: update sort algorithms
yanglbme Apr 28, 2020
429392a
feat: add javaScript solutions for lcof problems
matteokjh Apr 30, 2020
ebda970
Merge pull request #257 from matteokjh/feature/lcof_js
yanglbme Apr 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<p align="center">
<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>
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
<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>
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
<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>
Expand All @@ -18,25 +17,24 @@
[English Version](/README_EN.md)

## 站点
- Netlify: https://lc.netlify.app
- GitHub Pages: https://doocs.github.io/leetcode
- Coding Pages: https://d3jc40.coding-pages.com

注:若 [GitHub Pages](https://doocs.github.io/leetcode) 访问加载过慢,可访问 [Coding Pages](https://d3jc40.coding-pages.com)。

## 题解
- [LeetCode](/solution/README.md)
- [剑指 Offer(第 2 版)](/lcof/README.md)
- [程序员面试金典(第 6 版)](/lcci/README.md)
- [LeetCode 《剑指 Offer(第 2 版)](/lcof/README.md)
- [LeetCode 《程序员面试金典(第 6 版)](/lcci/README.md)

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

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

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

- 将[本项目](https://github.com/doocs/leetcode) <kbd>Fork</kbd> 到你的个人 GitHub 帐户,然后 <kbd>clone</kbd> 到你的本地机器;
- 将[本项目](https://github.com/doocs/leetcode) <kbd>fork</kbd> 到你的个人 GitHub 帐户,然后 <kbd>clone</kbd> 到你的本地机器;
- 对项目做出一些变更,然后使用 git <kbd>add</kbd>、<kbd>commit</kbd>、<kbd>push</kbd> 等命令将你的变更提交到你的远程 GitHub 仓库;
- 将你的变更以 PR 的形式提交过来,项目的维护人员会在第一时间对你的变更进行 review!
- 你也可以参考 [GitHub 帮助文档](https://help.github.com/cn) 了解更多细节。
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<p align="center">
<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>
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
<a href="https://github.com/doocs/leetcode/issues"><img src="https://badgen.net/github/open-issues/doocs/leetcode" alt="issues"></a>
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode" alt="forks"></a>
<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>
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>
<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>
Expand All @@ -18,6 +17,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
[中文文档](/README.md)

## Sites
- Netlify: https://lc.netlify.app
- GitHub Pages: https://doocs.github.io/leetcode
- Coding Pages: https://d3jc40.coding-pages.com

Expand Down
7 changes: 7 additions & 0 deletions basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 算法

## 常见的排序算法

- 冒泡排序
- 插入排序
- 归并排序
28 changes: 28 additions & 0 deletions basic/sort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;

public class BubbleSort {
private static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}

private static void bubbleSort(int[] nums) {
boolean hasChange = true;
for (int i = 0; i < nums.length - 1 && hasChange; ++i) {
hasChange = false;
for (int j = 0; j < nums.length - 1 - i; ++j) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
hasChange = true;
}
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 7, 9, 5, 8};
bubbleSort(nums);
System.out.println(Arrays.toString(nums));
}
}
19 changes: 19 additions & 0 deletions basic/sort/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;

public class InsertionSort {
private static void insertionSort(int[] nums) {
for (int i = 1, j, current; i < nums.length; ++i) {
current = nums[i];
for (j = i - 1; j >= 0 && nums[j] > current; --j) {
nums[j + 1] = nums[j];
}
nums[j + 1] = current;
}
}

public static void main(String[] args) {
int[] nums = { 1, 2, 7, 9, 5, 8 };
insertionSort(nums);
System.out.println(Arrays.toString(nums));
}
}
44 changes: 44 additions & 0 deletions basic/sort/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.Arrays;

public class MergeSort {

private static void merge(int[] nums, int low, int mid, int high, int[] temp) {
int i = low, j = mid + 1, k = low;
while (k <= high) {
if (i > mid) {
temp[k++] = nums[j++];
} else if (j > high) {
temp[k++] = nums[i++];
} else if (nums[i] <= nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}

for (i = low; i <= high; ++i) {
nums[i] = temp[i];
}
}

private static void mergeSort(int[] nums, int low, int high, int[] temp) {
if (low >= high) {
return;
}
int mid = low + ((high - low) >> 1);
mergeSort(nums, low, mid, temp);
mergeSort(nums, mid + 1, high, temp);
merge(nums, low, mid, high, temp);
}

private static void mergeSort(int[] nums) {
int[] temp = new int[nums.length];
mergeSort(nums, 0, nums.length - 1, temp);
}

public static void main(String[] args) {
int[] nums = {1, 2, 7, 4, 5, 3};
mergeSort(nums);
System.out.println(Arrays.toString(nums));
}
}
167 changes: 167 additions & 0 deletions basic/sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# 常见的排序算法

## 冒泡排序

定义一个布尔变量 `hasChange`,用来标记每轮是否进行了交换。在每轮遍历开始时,将 `hasChange` 设置为 false。

### 代码示例

```java
import java.util.Arrays;

public class BubbleSort {
private static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}

private static void bubbleSort(int[] nums) {
boolean hasChange = true;
for (int i = 0; i < nums.length - 1 && hasChange; ++i) {
hasChange = false;
for (int j = 0; j < nums.length - 1 - i; ++j) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
hasChange = true;
}
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 7, 9, 5, 8};
bubbleSort(nums);
System.out.println(Arrays.toString(nums));
}
}
```

### 算法分析

空间复杂度 O(1)、时间复杂度 O(n^2)。

分情况讨论:

1. 给定的数组按照顺序已经排好:只需要进行 `n-1` 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。
3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 O(n^2)。

因此,时间复杂度是 O(n^2),这是一种稳定的排序算法。

> 稳定是指,两个相等的数,在排序过后,相对位置保持不变。

## 插入排序

与冒泡排序对比:

- 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。
- 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的。

插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。

### 代码示例

```java
import java.util.Arrays;

public class InsertionSort {
private static void insertionSort(int[] nums) {
for (int i = 1, j, current; i < nums.length; ++i) {
current = nums[i];
for (j = i - 1; j >= 0 && nums[j] > current; --j) {
nums[j + 1] = nums[j];
}
nums[j + 1] = current;
}
}

public static void main(String[] args) {
int[] nums = { 1, 2, 7, 9, 5, 8 };
insertionSort(nums);
System.out.println(Arrays.toString(nums));
}
}
```

### 算法分析

空间复杂度 O(1),时间复杂度 O(n^2)。

分情况讨论:

1. 给定的数组按照顺序排好序:只需要进行 n-1 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。
3. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n^2)。

因此,时间复杂度是 O(n^2),这也是一种稳定的排序算法。

## 归并排序

归并排序的核心思想是分治,把一个复杂问题拆分成若干个子问题来求解。

归并排序的算法思想是:把数组从中间划分为两个子数组,一直递归地把子数组划分成更小的数组,直到子数组里面只有一个元素的时候开始排序。排序的方法就是按照大小顺序合并两个元素。接着依次按照递归的顺序返回,不断合并排好序的数组,直到把整个数组排好序。

### 代码示例

```java
import java.util.Arrays;

public class MergeSort {

private static void merge(int[] nums, int low, int mid, int high, int[] temp) {
int i = low, j = mid + 1, k = low;
while (k <= high) {
if (i > mid) {
temp[k++] = nums[j++];
} else if (j > high) {
temp[k++] = nums[i++];
} else if (nums[i] <= nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}

for (i = low; i <= high; ++i) {
nums[i] = temp[i];
}
}

private static void mergeSort(int[] nums, int low, int high, int[] temp) {
if (low >= high) {
return;
}
int mid = low + ((high - low) >> 1);
mergeSort(nums, low, mid, temp);
mergeSort(nums, mid + 1, high, temp);
merge(nums, low, mid, high, temp);
}

private static void mergeSort(int[] nums) {
int n = nums.length;
int[] temp = new int[n];
mergeSort(nums, 0, n - 1, temp);
}

public static void main(String[] args) {
int[] nums = {1, 2, 7, 4, 5, 3};
mergeSort(nums);
System.out.println(Arrays.toString(nums));
}
}
```

### 算法分析

空间复杂度 O(n),时间复杂度 O(nlogn)。

对于规模为 n 的问题,一共要进行 log(n) 次的切分,每一层的合并复杂度都是 O(n),所以整体时间复杂度为 O(nlogn)。

由于合并 n 个元素需要分配一个大小为 n 的额外数组,所以空间复杂度为 O(n)。

这是一种稳定的排序算法。

## 快速排序

快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。
23 changes: 10 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,27 @@
}
},
plugins: [
function (hook) {
var footer = [
'<hr/>',
'<footer>',
'<span>Copyright © 2018-2020 <a href="https://github.com/doocs" target="_blank">Doocs</a>. All rights reserved.',
'</footer>'
].join('')

hook.afterEach(function (html) {
return html + footer
})
},
function (hook, vm) {
hook.beforeEach(function (html) {
const en = vm.route.file.indexOf('README_EN') > -1
if (/githubusercontent\.com/.test(vm.route.file)) {
url = vm.route.file
.replace('raw.githubusercontent.com', 'github.com')
.replace(/\/master/, '/blob/master')
} else {
url = 'https://github.com/doocs/leetcode/blob/master/' + vm.route.file
}
var editHtml = '[:memo: Edit on GitHub](' + url + ')\n'
const edit = en ? 'Edit on GitHub' : '在 GitHub 编辑'
const editHtml = `[:memo: ${edit}](${url})\n`
return editHtml + html
})

hook.afterEach(function (html) {
const en = vm.route.file.indexOf('README_EN') != -1
const copyright = en ? '. All Rights Reserved' : ' 版权所有'
const footer = `<footer><span>Copyright © 2018-2020 <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`
return html + footer
})
},
]
}
Expand Down
Loading