Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc6f1a9

Browse files
committed
新增排序和二分部分题目
1 parent d52018d commit bc6f1a9

27 files changed

+856
-148
lines changed

‎README.md‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@
3030
| 34 | [在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [searchRange](./bsearch/leetcode/medium/searchRange.h) | <font color=orange> medium </font> ||
3131
| 35 | [搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/) | [searchInsert](./array/leetcode/easy/searchInsert.h) | <font color=green>easy</font> ||
3232
| 36 | [有效的数独](https://leetcode-cn.com/problems/valid-sudoku/)| [isValidSudoku](./other/leetcode/medium/isValidSudoku.h) | <font color=orange> medium </font> ||
33-
| 39 | [组合总和](https://leetcode-cn.com/problems/combination-sum/)| [combinationSum](./array/leetcode/medium/combinationSum.h) | <font color=orange> medium </font> ||
33+
| 39 | [组合总和](https://leetcode-cn.com/problems/combination-sum/)| [combinationSum](./backtracking/leetcode/medium/combinationSum.h) | <font color=orange> medium </font> ||
34+
| 46 | [全排列](https://leetcode-cn.com/problems/permutations/)| [permutations](./backtracking/leetcode/medium/permutations.h) | <font color=orange> medium </font> ||
3435
| 53 | [最大子序和](https://leetcode-cn.com/problems/maximum-subarray/) | [maxSubArray](./array/leetcode/easy/maxSubArray.h) | <font color=green>easy</font> ||
36+
| 56 | [合并区间](https://leetcode-cn.com/problems/merge-intervals/) | [merge_intervals](./sort/leetcode/merge_intervals.h) | <font color=orange> medium </font> ||
3537
| 58| [最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word) | [lengthOfLastWord](./string/leetcode/easy/lengthOfLastWord.h) | <font color=green>easy</font> ||
3638
| 66 | [加一](https://leetcode-cn.com/problems/plus-one/) | [plusOne](./array/leetcode/easy/plusOne.h) | <font color=green>easy</font> ||
3739
| 67 | [二进制求和](https://leetcode-cn.com/problems/add-binary/) | [addBinary](./array/leetcode/easy/addBinary.h) | <font color=green>easy</font> ||
3840
| 69 | [x 的平方根](https://leetcode-cn.com/problems/sqrtx/%E2%80%A8)| [mySqrt](./bsearch/leetcode/mySqrt.h) | <font color=green>easy</font> ||
3941
| 70 | [爬楼梯](https://leetcode-cn.com/problems/climbing-stairs/)| [climbStairs](./dp/leetcode/easy/climbStairs.h) | <font color=green>easy</font> ||
42+
| 75 | [颜色分类](https://leetcode-cn.com/problems/sort-colors/) | [sort_colors](./sort/leetcode/sort_colors.h) | <font color=orange> medium </font> ||
4043
| 83 | [删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)| [deleteDuplicates](./linkedList/leetcode/easy/deleteDuplicates.h) | <font color=green>easy</font> ||
4144
| 88 | [合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) | [merge](./array/leetcode/easy/merge.h) | <font color=green>easy</font> ||
4245
| 100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [isSameTree](./tree/leetcode/easy/isSameTree.h) | <font color=green>easy</font> ||
46+
| 687 | [最长同值路径](https://leetcode-cn.com/problems/longest-univalue-path/)| [longestUnivaluePath](./recursion/leetcode/medium/longestUnivaluePath.h) | <font color=green>easy</font> ||
47+
48+
49+
50+
4351

4452
----------------
4553
-----------------
@@ -55,6 +63,39 @@
5563

5664
## [递归](./recursion.md) 🚶🚶🚶🚶
5765

66+
### 什么是递归?
67+
68+
69+
试想一下电影院自己在第几排的场景, 想要知道我在第几排,那么就要去问前面的人他处于第几排,那么前面的人又怎么知道他自己在第几排呢?那自然是他也要去问他前面的人咯.
70+
71+
这就是递归的场景,去的过程叫做<font size=5 color=red>"递"</font>, 回来的过程叫做<font size=5 color=red>"归"</font>,有来有回。
72+
73+
74+
![](./res/recursion_example.png)
75+
76+
<font size=5 color=red>递归其实就是利用栈的数据结构,再加上一些简单的逻辑算法从而完成了问题的求解。只不过这个栈是由系统来提供的,我们只是无感知罢了.</font>
77+
78+
79+
80+
### 适合场景
81+
82+
满足下面两点:
83+
84+
* 一个问题的解可以分解为几个子问题(规模更小的问题)的解, 并且子问题和问题除了数据规模不一样,求解思路是完全一样的。
85+
* 存在终止条件
86+
87+
88+
那么如何编写递归代码呢?
89+
90+
关键在于找到将大问题分解为小问题的规律,写出递归公式,然后在推敲出终止条件。
91+
92+
递归代码简洁高效,但是容易出现堆栈溢出,重复计算,函数调用耗时多,空间复杂度高等问题。一般嵌套比较少的场景可以使用递归。
93+
94+
95+
### [递归题目](./recursion.md)
96+
97+
98+
5899
## [排序](./sort.md)
59100

60101
## [二分查找](./bsearch.md)
@@ -123,7 +164,7 @@
123164
* 二分查找
124165

125166

126-
## [链表](./linkedlist.md)
167+
## [链表](./linkedList.md)
127168
## [栈&队列](./stack_queue.md)
128169

129170
## [](./tree.md)

‎alg-cpp.xcodeproj/project.pbxproj‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@
163163
/* Begin PBXFileReference section */
164164
09132D88230AECBF0017E435 /* integer_to_roman.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = integer_to_roman.h; sourceTree = "<group>"; };
165165
09132D8C230AF7140017E435 /* 3sum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 3sum.h; sourceTree = "<group>"; };
166+
091A5E14231EB6FC000BEFAF /* merge_intervals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = merge_intervals.h; sourceTree = "<group>"; };
167+
091A5E17231EC2AE000BEFAF /* sort_colors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sort_colors.h; sourceTree = "<group>"; };
166168
091B8A7122F48FD50050FB4C /* IsBalanced_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IsBalanced_Solution.h; sourceTree = "<group>"; };
167169
0922310622FA445300F0F5AF /* removeElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = removeElement.h; sourceTree = "<group>"; };
168170
0922310922FA497500F0F5AF /* searchInsert.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchInsert.h; sourceTree = "<group>"; };
@@ -215,14 +217,17 @@
215217
0998EB8A22E75FB9005A01B5 /* dp */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = dp; sourceTree = BUILT_PRODUCTS_DIR; };
216218
0998EB8C22E75FB9005A01B5 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
217219
0998EB9122E75FEC005A01B5 /* knapsack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = knapsack.h; sourceTree = "<group>"; };
220+
099E1B2923200FC2000C4FC3 /* bsearch_findFirstElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bsearch_findFirstElement.h; sourceTree = "<group>"; };
221+
099E1B2C2320142D000C4FC3 /* bsearch_findLastElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bsearch_findLastElement.h; sourceTree = "<group>"; };
222+
099E1B2F23201572000C4FC3 /* bsearch_findFirstElementGreaterOrEqual.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bsearch_findFirstElementGreaterOrEqual.h; sourceTree = "<group>"; };
223+
099E1B322320175F000C4FC3 /* bsearch_findLastElementLessOrEqual.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bsearch_findLastElementLessOrEqual.h; sourceTree = "<group>"; };
218224
099EAEF022FFB55A006437BD /* MinStack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MinStack.h; sourceTree = "<group>"; };
219225
099EAEF522FFBED0006437BD /* reverse_integer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = reverse_integer.h; sourceTree = "<group>"; };
220226
099EAEF822FFC4C5006437BD /* palindrome_number.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = palindrome_number.h; sourceTree = "<group>"; };
221227
09A208B02308D96E00094088 /* zigzag_conversion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = zigzag_conversion.h; sourceTree = "<group>"; };
222228
09A208B32309990C00094088 /* string_to_integer_atoi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = string_to_integer_atoi.h; sourceTree = "<group>"; };
223229
09ABED832300E22800113589 /* romanToInt.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = romanToInt.h; sourceTree = "<group>"; };
224230
09ABED872300EE0900113589 /* longestCommonPrefix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = longestCommonPrefix.h; sourceTree = "<group>"; };
225-
09ACF0242316D03F004A4A61 /* combinationSum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = combinationSum.h; sourceTree = "<group>"; };
226231
09ACF02B2316DB0B004A4A61 /* divideandconquer */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = divideandconquer; sourceTree = BUILT_PRODUCTS_DIR; };
227232
09ACF02D2316DB0B004A4A61 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
228233
09ACF0322316DB2C004A4A61 /* reversedOrderPairs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = reversedOrderPairs.h; sourceTree = "<group>"; };
@@ -231,6 +236,10 @@
231236
09AE483F22EDED23003B4450 /* rotate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = rotate.h; sourceTree = "<group>"; };
232237
09BEFF3622E141D300FF7CD3 /* kthSmallest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = kthSmallest.h; sourceTree = "<group>"; };
233238
09C5E0D82301C30300EDD63B /* strStr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = strStr.h; sourceTree = "<group>"; };
239+
09C8705F231D5EBE00ABE173 /* bucketSort.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bucketSort.h; sourceTree = "<group>"; };
240+
09CA9FAD2318A7A500BE2B39 /* combinationSum.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = combinationSum.h; sourceTree = "<group>"; };
241+
09CA9FAE2318A7A500BE2B39 /* permutations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = permutations.h; sourceTree = "<group>"; };
242+
09CA9FAF2319744400BE2B39 /* longestUnivaluePath.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = longestUnivaluePath.h; sourceTree = "<group>"; };
234243
09CB5B9C22E0B5E6007A1133 /* mergeSort.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mergeSort.h; sourceTree = "<group>"; };
235244
09CB5B9F22E0C70D007A1133 /* quickSort.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = quickSort.h; sourceTree = "<group>"; };
236245
09E18E4422F514A0002D0227 /* GetNext.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetNext.h; sourceTree = "<group>"; };
@@ -452,11 +461,19 @@
452461
children = (
453462
09132D8C230AF7140017E435 /* 3sum.h */,
454463
09771DA0230C44C5000F8AC3 /* 3SumClosest.h */,
455-
09ACF0242316D03F004A4A61 /* combinationSum.h */,
456464
);
457465
path = medium;
458466
sourceTree = "<group>";
459467
};
468+
091A5E13231EB6E4000BEFAF /* leetcode */ = {
469+
isa = PBXGroup;
470+
children = (
471+
091A5E14231EB6FC000BEFAF /* merge_intervals.h */,
472+
091A5E17231EC2AE000BEFAF /* sort_colors.h */,
473+
);
474+
path = leetcode;
475+
sourceTree = "<group>";
476+
};
460477
0924B8E522F9D88E00435980 /* easy */ = {
461478
isa = PBXGroup;
462479
children = (
@@ -510,6 +527,7 @@
510527
isa = PBXGroup;
511528
children = (
512529
09421FF2230EE57F00A7BA67 /* generateParenthesis.h */,
530+
09CA9FAF2319744400BE2B39 /* longestUnivaluePath.h */,
513531
);
514532
path = medium;
515533
sourceTree = "<group>";
@@ -618,6 +636,8 @@
618636
09771DA3230C4C8D000F8AC3 /* medium */ = {
619637
isa = PBXGroup;
620638
children = (
639+
09CA9FAD2318A7A500BE2B39 /* combinationSum.h */,
640+
09CA9FAE2318A7A500BE2B39 /* permutations.h */,
621641
09771DA4230C4CAA000F8AC3 /* letter_combinations_of_a_phone_number.h */,
622642
);
623643
path = medium;
@@ -808,13 +828,15 @@
808828
3A5C8BB922E06F6900354740 /* sort */ = {
809829
isa = PBXGroup;
810830
children = (
831+
091A5E13231EB6E4000BEFAF /* leetcode */,
811832
3A5C8BBA22E06F6900354740 /* main.cpp */,
812833
3A5C8BBF22E0795500354740 /* bubbleSort.h */,
813834
3A5C8BC222E0879C00354740 /* insertSort.h */,
814835
3A5C8BC522E08CFB00354740 /* selectSort.h */,
815836
09CB5B9C22E0B5E6007A1133 /* mergeSort.h */,
816837
09CB5B9F22E0C70D007A1133 /* quickSort.h */,
817838
09BEFF3622E141D300FF7CD3 /* kthSmallest.h */,
839+
09C8705F231D5EBE00ABE173 /* bucketSort.h */,
818840
3A5C8BC822E16B8600354740 /* countingSort.h */,
819841
3A6CF75D22E5BEE0004B8775 /* heapSort.h */,
820842
);
@@ -827,6 +849,10 @@
827849
3A5C8BDC22E1CE3500354740 /* leetcode */,
828850
3A5C8BD122E1A7AE00354740 /* main.cpp */,
829851
3A5C8BD622E1A7B900354740 /* bsearch.h */,
852+
099E1B2923200FC2000C4FC3 /* bsearch_findFirstElement.h */,
853+
099E1B2C2320142D000C4FC3 /* bsearch_findLastElement.h */,
854+
099E1B2F23201572000C4FC3 /* bsearch_findFirstElementGreaterOrEqual.h */,
855+
099E1B322320175F000C4FC3 /* bsearch_findLastElementLessOrEqual.h */,
830856
);
831857
path = bsearch;
832858
sourceTree = "<group>";
13.5 KB
Binary file not shown.

‎backtracking.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
| &emsp;题号&emsp; | 题目链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| 答案链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| &emsp;难度&emsp; | &emsp;完成度&emsp; |
1414
| :--: | :--: | :----------------------------------------------------------- | :----------------------------------------------------------- | :------: |
1515
| 17 | [电话号码的字母组合](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/) | [letter_combinations_of_a_phone_number](./backtracking/leetcode/medium/letter_combinations_of_a_phone_number.h) | <font color=orange> medium </font> ||
16+
| 39 | [组合总和](https://leetcode-cn.com/problems/combination-sum/)| [combinationSum](./backtracking/leetcode/medium/combinationSum.h) | <font color=orange> medium </font> ||
17+
| 46 | [全排列](https://leetcode-cn.com/problems/permutations/)| [permutations](./backtracking/leetcode/medium/permutations.h) | <font color=orange> medium </font> ||
1618

1719

1820
### 剑指Offer
File renamed without changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// permutations.h
3+
// recursion
4+
//
5+
// Created by junlongj on 2019年8月30日.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef permutations_hpp
10+
#define permutations_hpp
11+
12+
#include <stdio.h>
13+
#include <vector>
14+
/*
15+
46.全排列
16+
给定一个没有重复数字的序列,返回其所有可能的全排列。
17+
18+
示例:
19+
20+
输入: [1,2,3]
21+
输出:
22+
[
23+
[1,2,3],
24+
[1,3,2],
25+
[2,1,3],
26+
[2,3,1],
27+
[3,1,2],
28+
[3,2,1]
29+
]
30+
31+
来源:力扣(LeetCode)
32+
链接:https://leetcode-cn.com/problems/permutations
33+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
34+
*/
35+
namespace leetcode {
36+
using namespace std;
37+
class Solution_46 {
38+
public:
39+
vector<vector<int>> permute(vector<int>& nums) {
40+
vector<int> elements;
41+
permute(nums, elements);
42+
return results;
43+
}
44+
private:
45+
vector<vector<int>> results;
46+
void permute(vector<int>& nums, vector<int> elements) {
47+
//终止条件
48+
if (elements.size() == nums.size()){
49+
results.push_back(elements);
50+
return;
51+
}
52+
//递归公式
53+
for (auto &num : nums){
54+
if(find(elements.begin(),elements.end(),num) != elements.end())
55+
continue;
56+
elements.push_back(num);
57+
permute(nums, elements);
58+
elements.pop_back();
59+
}
60+
}
61+
};
62+
}
63+
64+
#endif /* permutations_hpp */

‎bsearch.md‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1-
## 二分查找
2-
### leetcode
1+
# 二分查找
2+
3+
二分查找是在<font size=5 color=red>有序集合中快速寻找某个元素</font>的算法, 它的时间复杂度为O(logn)是一种非常高效的查找算法。<font size=5 color=red>二分查找每次都通过和区间的中间元素作对比,来将查找区间缩小为之前的一半,直到找到元素或区间缩小为一.</font>
4+
5+
6+
7+
二分适合的场景:
8+
9+
1. 二分查找依赖于数组结构的随机访问能力,对于其他数据结构无法适用。
10+
2. 数组里面的元素必须是有序的,即如果我们是一次排序N次二分查找,那么可以平摊掉排序的时间,如果数据集合是频繁插入,删除,那么每次二分之前就需要先排序,在查找了,二分就不适合了.
11+
3. 数据量太小不适合用二分查找,比如10次以内的循环,其实直接顺序遍历时间也差不多。但是如果涉及到元素比较比较耗时(比如长字符串直接的比较),那么比较次数越少越好
12+
4. 数据量太大也不适用于二分查找。因为二分需要依靠数组,而数组是无法存储大数据的。
13+
14+
15+
16+
## [二分查找基本代码](./bsearch/bsearch.h)
17+
18+
二分查找变形问题
19+
20+
## [查找第一个值等于给定值的元素](./bsearch/bsearch_findFirstElement.h)
21+
22+
## [查找最后一个值等于给定值的元素](./bsearch/bsearch_findLastElement.h)
23+
24+
## [ 查找第一个大于等于指定元素的值](./bsearch/bsearch_findFirstElementGreaterOrEqual.h)
25+
## [查找最后一个小于等于给定值的元素](./bsearch/bsearch_findLastElementLessOrEqual.h)
26+
27+
28+
在排序数组中寻找一个指定的数,我们既可以使用二分,也可以使用二叉树,散列表等数据结构。虽然后者会使用更多内存,但是当内存不是如此紧缺的情况下,使用后者也能做到。那么二分的优势何在呢?
29+
30+
31+
<font size=5 color=red>二分法更适合用于"近似查找"问题</font>,比如刚刚的几个变种问题,如果使用散列表等结构就做不到了。
32+
33+
34+
## leetcode
335
| &emsp;题号&emsp; | 题目链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| 答案链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| &emsp;难度&emsp; | &emsp;完成度&emsp; |
436
| :--: | :--: | :----------------------------------------------------------- | :----------------------------------------------------------- | :------: |
537
| 33 | [搜索旋转排序数组](https://leetcode-cn.com/problems/search-in-rotated-sorted-array)| [searchInRotatedSortedArray](./bsearch/leetcode/medium/searchInRotatedSortedArray.h) | ✨✨ ||

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /