Skip to content

Commit 53adae2

Browse files
committed
新增二分题目
1 parent bc6f1a9 commit 53adae2

File tree

11 files changed

+422
-45
lines changed

11 files changed

+422
-45
lines changed

README.md

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@
3232
| 36 | [有效的数独](https://leetcode-cn.com/problems/valid-sudoku/)| [isValidSudoku](./other/leetcode/medium/isValidSudoku.h) | <font color=orange> medium </font> ||
3333
| 39 | [组合总和](https://leetcode-cn.com/problems/combination-sum/)| [combinationSum](./backtracking/leetcode/medium/combinationSum.h) | <font color=orange> medium </font> ||
3434
| 46 | [全排列](https://leetcode-cn.com/problems/permutations/)| [permutations](./backtracking/leetcode/medium/permutations.h) | <font color=orange> medium </font> ||
35+
| 50 | [Pow(x, n)](https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode/) | [powx](./bsearch/leetcode/medium/powx.h) | <font color=orange> medium </font> ||
3536
| 53 | [最大子序和](https://leetcode-cn.com/problems/maximum-subarray/) | [maxSubArray](./array/leetcode/easy/maxSubArray.h) | <font color=green>easy</font> ||
3637
| 56 | [合并区间](https://leetcode-cn.com/problems/merge-intervals/) | [merge_intervals](./sort/leetcode/merge_intervals.h) | <font color=orange> medium </font> ||
3738
| 58| [最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word) | [lengthOfLastWord](./string/leetcode/easy/lengthOfLastWord.h) | <font color=green>easy</font> ||
3839
| 66 | [加一](https://leetcode-cn.com/problems/plus-one/) | [plusOne](./array/leetcode/easy/plusOne.h) | <font color=green>easy</font> ||
3940
| 67 | [二进制求和](https://leetcode-cn.com/problems/add-binary/) | [addBinary](./array/leetcode/easy/addBinary.h) | <font color=green>easy</font> ||
4041
| 69 | [x 的平方根](https://leetcode-cn.com/problems/sqrtx/%E2%80%A8)| [mySqrt](./bsearch/leetcode/mySqrt.h) | <font color=green>easy</font> ||
4142
| 70 | [爬楼梯](https://leetcode-cn.com/problems/climbing-stairs/)| [climbStairs](./dp/leetcode/easy/climbStairs.h) | <font color=green>easy</font> ||
43+
| 74 | [搜索二维矩阵](https://leetcode-cn.com/problems/search-a-2d-matrix/) | [search_a_2d_matrix](./bsearch/leetcode/medium/search_a_2d_matrix.h) | <font color=orange> medium </font> ||
4244
| 75 | [颜色分类](https://leetcode-cn.com/problems/sort-colors/) | [sort_colors](./sort/leetcode/sort_colors.h) | <font color=orange> medium </font> ||
45+
| 81 | [搜索旋转排序数组II](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/) | [searchInRotatedSortedArrayII](./bsearch/leetcode/medium/searchInRotatedSortedArrayII.h) | <font color=orange> medium </font> ||
4346
| 83 | [删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)| [deleteDuplicates](./linkedList/leetcode/easy/deleteDuplicates.h) | <font color=green>easy</font> ||
4447
| 88 | [合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) | [merge](./array/leetcode/easy/merge.h) | <font color=green>easy</font> ||
4548
| 100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [isSameTree](./tree/leetcode/easy/isSameTree.h) | <font color=green>easy</font> ||
49+
| 153 | [寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) | [find_minimum_in_rotated_sorted_array](./bsearch/leetcode/medium/find_minimum_in_rotated_sorted_array.h) | <font color=orange> medium </font> ||
4650
| 687 | [最长同值路径](https://leetcode-cn.com/problems/longest-univalue-path/)| [longestUnivaluePath](./recursion/leetcode/medium/longestUnivaluePath.h) | <font color=green>easy</font> ||
4751

4852

@@ -61,47 +65,14 @@
6165

6266
# **算法思想**
6367

64-
## [递归](./recursion.md) 🚶🚶🚶🚶
65-
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-
68+
## [递归](./recursion.md)
9869

9970
## [排序](./sort.md)
10071

101-
## [二分查找](./bsearch.md)
72+
## [二分查找](./bsearch.md) 🚶🚶🚶🚶
10273

10374

104-
# 解决多阶段决策最优解模型的算法
75+
<!--# 解决多阶段决策最优解模型的算法
10576
解决问题的过程中,需要经过多个决策阶段,每个决策都会对应一个状态。我们寻找一组决策序列,经过这组决策序列,能够产生最终期望的最优值。我们把这种问题模型称为<font size=5 color=red>多阶段决策最优解模型</font>. DP,回溯,贪心都可以解决这类问题.
10677
10778
利用动态规划解决的问题,需要满足三个特征:
@@ -119,7 +90,7 @@
11990
所以贪心算法能否解决算法问题的关键在于: 局部最优能不能达到全局最优?
12091
12192
122-
回溯算法是”万金油“。基本上贪心和dp能解决的问题,回溯都能解决。回溯相当于穷举搜索,列举出所有的情况,然后对比得到最优解。不过回溯的复杂度一般都是指数级的,只能用来解决小规模数据的问题。
93+
回溯算法是”万金油“。基本上贪心和dp能解决的问题,回溯都能解决。回溯相当于穷举搜索,列举出所有的情况,然后对比得到最优解。不过回溯的复杂度一般都是指数级的,只能用来解决小规模数据的问题。-->
12394

12495

12596
## [动态规划](./dp.md)

alg-cpp.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,19 @@
201201
0946B05322F6E6E10043469D /* printMatrix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = printMatrix.h; sourceTree = "<group>"; };
202202
0946B05622F706A00043469D /* GetLeastNumbers_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetLeastNumbers_Solution.h; sourceTree = "<group>"; };
203203
0946B05922F711AC0043469D /* NumberOf1Between1AndN_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NumberOf1Between1AndN_Solution.h; sourceTree = "<group>"; };
204+
095F60122322B3390072CF0C /* searchInRotatedSortedArrayII.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchInRotatedSortedArrayII.h; sourceTree = "<group>"; };
204205
0965B3CB23045219009A153E /* lengthOfLastWord.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lengthOfLastWord.h; sourceTree = "<group>"; };
205206
0965B3CE23045B1C009A153E /* addBinary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = addBinary.h; sourceTree = "<group>"; };
206207
0965B3D223046663009A153E /* climbStairs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = climbStairs.h; sourceTree = "<group>"; };
207208
0965B3D523046775009A153E /* romanToInt.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = romanToInt.h; sourceTree = "<group>"; };
208209
0969A71B22E607E800CA9347 /* string */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = string; sourceTree = BUILT_PRODUCTS_DIR; };
209210
0969A71D22E607E800CA9347 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
210211
0969A72222E6081500CA9347 /* matching.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = matching.h; sourceTree = "<group>"; };
212+
096E161723215E7300444948 /* powx.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = powx.h; sourceTree = "<group>"; };
213+
096E161A2321681C00444948 /* search_a_2d_matrix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = search_a_2d_matrix.h; sourceTree = "<group>"; };
211214
09771DA0230C44C5000F8AC3 /* 3SumClosest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 3SumClosest.h; sourceTree = "<group>"; };
212215
09771DA4230C4CAA000F8AC3 /* letter_combinations_of_a_phone_number.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = letter_combinations_of_a_phone_number.h; sourceTree = "<group>"; };
216+
098F84CC23234824001219CB /* find_minimum_in_rotated_sorted_array.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = find_minimum_in_rotated_sorted_array.h; sourceTree = "<group>"; };
213217
0993E17022F3282B001E4308 /* minDepth.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = minDepth.h; sourceTree = "<group>"; };
214218
0993E17322F32CC0001E4308 /* hasPathSum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = hasPathSum.h; sourceTree = "<group>"; };
215219
0993E17622F33B78001E4308 /* preorderTraversal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = preorderTraversal.h; sourceTree = "<group>"; };
@@ -545,7 +549,11 @@
545549
isa = PBXGroup;
546550
children = (
547551
3A5C8BDD22E1CEC400354740 /* searchInRotatedSortedArray.h */,
552+
095F60122322B3390072CF0C /* searchInRotatedSortedArrayII.h */,
553+
098F84CC23234824001219CB /* find_minimum_in_rotated_sorted_array.h */,
548554
09421FFA23142AEB00A7BA67 /* searchRange.h */,
555+
096E161723215E7300444948 /* powx.h */,
556+
096E161A2321681C00444948 /* search_a_2d_matrix.h */,
549557
);
550558
path = medium;
551559
sourceTree = "<group>";
5.94 KB
Binary file not shown.

alg-cpp.xcodeproj/xcuserdata/junlongj.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
<key>base.xcscheme_^#shared#^_</key>
2525
<dict>
2626
<key>orderHint</key>
27-
<integer>7</integer>
27+
<integer>5</integer>
2828
</dict>
2929
<key>bsearch.xcscheme_^#shared#^_</key>
3030
<dict>
3131
<key>orderHint</key>
32-
<integer>8</integer>
32+
<integer>9</integer>
3333
</dict>
3434
<key>divideandconquer.xcscheme_^#shared#^_</key>
3535
<dict>
@@ -39,17 +39,17 @@
3939
<key>dp.xcscheme_^#shared#^_</key>
4040
<dict>
4141
<key>orderHint</key>
42-
<integer>12</integer>
42+
<integer>10</integer>
4343
</dict>
4444
<key>hasTable.xcscheme_^#shared#^_</key>
4545
<dict>
4646
<key>orderHint</key>
47-
<integer>10</integer>
47+
<integer>11</integer>
4848
</dict>
4949
<key>linkedList.xcscheme_^#shared#^_</key>
5050
<dict>
5151
<key>orderHint</key>
52-
<integer>9</integer>
52+
<integer>6</integer>
5353
</dict>
5454
<key>other.xcscheme_^#shared#^_</key>
5555
<dict>
@@ -64,12 +64,12 @@
6464
<key>sort.xcscheme_^#shared#^_</key>
6565
<dict>
6666
<key>orderHint</key>
67-
<integer>5</integer>
67+
<integer>8</integer>
6868
</dict>
6969
<key>stack+queue.xcscheme_^#shared#^_</key>
7070
<dict>
7171
<key>orderHint</key>
72-
<integer>6</integer>
72+
<integer>7</integer>
7373
</dict>
7474
<key>string.xcscheme_^#shared#^_</key>
7575
<dict>
@@ -79,7 +79,7 @@
7979
<key>tree.xcscheme_^#shared#^_</key>
8080
<dict>
8181
<key>orderHint</key>
82-
<integer>11</integer>
82+
<integer>12</integer>
8383
</dict>
8484
</dict>
8585
<key>SuppressBuildableAutocreation</key>

bsearch.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@
3535
| &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; |
3636
| :--: | :--: | :----------------------------------------------------------- | :----------------------------------------------------------- | :------: |
3737
| 33 | [搜索旋转排序数组](https://leetcode-cn.com/problems/search-in-rotated-sorted-array)| [searchInRotatedSortedArray](./bsearch/leetcode/medium/searchInRotatedSortedArray.h) | ✨✨ ||
38+
| 50 | [Pow(x, n)](https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode/) | [powx](./bsearch/leetcode/medium/powx.h) | <font color=orange> medium </font> ||
3839
| 69 | [x 的平方根](https://leetcode-cn.com/problems/sqrtx/%E2%80%A8)| [mySqrt](./bsearch/leetcode/mySqrt.h) |||
40+
| 74 | [搜索二维矩阵](https://leetcode-cn.com/problems/search-a-2d-matrix/) | [search_a_2d_matrix](./bsearch/leetcode/medium/search_a_2d_matrix.h) | <font color=orange> medium </font> ||
41+
| 81 | [搜索旋转排序数组II](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/) | [searchInRotatedSortedArrayII](./bsearch/leetcode/medium/searchInRotatedSortedArrayII.h) | <font color=orange> medium </font> ||
42+
| 153 | [寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) | [find_minimum_in_rotated_sorted_array](./bsearch/leetcode/medium/find_minimum_in_rotated_sorted_array.h) | <font color=orange> medium </font> ||
3943
| 367 | [有效的完全平方数](https://leetcode-cn.com/problems/valid-perfect-square/)| [isPerfectSquare](./bsearch/leetcode/isPerfectSquare.h) |||
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// find_minimum_in_rotated_sorted_array.h
3+
// bsearch
4+
//
5+
// Created by junl on 2019/9/7.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef find_minimum_in_rotated_sorted_array_hpp
10+
#define find_minimum_in_rotated_sorted_array_hpp
11+
12+
#include <stdio.h>
13+
/*
14+
153.寻找旋转排序数组中的最小值
15+
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
16+
17+
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
18+
19+
请找出其中最小的元素。
20+
21+
你可以假设数组中不存在重复元素。
22+
23+
示例 1:
24+
25+
输入: [3,4,5,1,2]
26+
输出: 1
27+
示例 2:
28+
29+
输入: [4,5,6,7,0,1,2]
30+
输出: 0
31+
32+
来源:力扣(LeetCode)
33+
链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array
34+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35+
*/
36+
37+
namespace leetcode {
38+
using namespace std;
39+
/*
40+
思路:
41+
寻找波谷,即寻找数组相邻两个元素,前一个元素比后一个元素大的位置.
42+
43+
1.我们首先判断[left....right]这部分是否已经是有序数组了,如果是有序数组,那么直接返回左边的数。
44+
2.如果不是有序的,那么久存在旋转的部分,最小数字肯定出现在旋转部分。
45+
46+
note:注意寻找波谷的条件nums[mid-1] > nums[mid] || nums[mid] > nums[mid+1]
47+
*/
48+
int findMin(vector<int>& nums) {
49+
if (nums.empty()) return -1;
50+
if (nums.size()==1) return nums[0];
51+
52+
int left=0;
53+
int right=nums.size()-1;
54+
while(left<=right){
55+
int mid = left+((right-left)>>1);
56+
//如果已经是有序部分,返回左节点
57+
if (nums[left] <= nums[mid] && nums[mid] <= nums[right]){
58+
return nums[left];
59+
}
60+
//寻找波谷
61+
if (mid >0 && nums[mid-1] > nums[mid]){
62+
return nums[mid];
63+
}
64+
if (mid +1 <nums.size() && nums[mid] > nums[mid+1]) {
65+
return nums[mid+1];
66+
}
67+
//查找哪半边是有序的
68+
if (nums[mid] > nums[left]){
69+
//左边是有序的
70+
left = mid + 1;
71+
}else{
72+
//右边是有序的
73+
right = mid - 1;
74+
}
75+
}
76+
return -1;
77+
}
78+
}
79+
#endif /* find_minimum_in_rotated_sorted_array_hpp */

bsearch/leetcode/medium/powx.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// powx.h
3+
// bsearch
4+
//
5+
// Created by junl on 2019/9/5.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef powx_hpp
10+
#define powx_hpp
11+
12+
#include <stdio.h>
13+
/*
14+
实现 pow(x, n) ,即计算 x 的 n 次幂函数。
15+
16+
示例 1:
17+
18+
输入: 2.00000, 10
19+
输出: 1024.00000
20+
示例 2:
21+
22+
输入: 2.10000, 3
23+
输出: 9.26100
24+
示例 3:
25+
26+
输入: 2.00000, -2
27+
输出: 0.25000
28+
解释: 2-2 = 1/22 = 1/4 = 0.25
29+
说明:
30+
31+
-100.0 < x < 100.0
32+
n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。
33+
34+
来源:力扣(LeetCode)
35+
链接:https://leetcode-cn.com/problems/powx-n
36+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
37+
*/
38+
39+
namespace leetcode {
40+
/*
41+
思路:
42+
常规思路,O(n)循环,每次乘以x,当n很大时,会超时.
43+
*/
44+
//bad
45+
double myPow2(double x, int n){
46+
long N = n; //这里用long来保存,因为-n可能会导致数据溢出。
47+
if (N < 0) {
48+
x = 1 / x;
49+
N = -N;
50+
}
51+
double ans = 1;
52+
for (long i = 0; i < N; i++)
53+
ans = ans * x;
54+
return ans;
55+
}
56+
/*
57+
思路二:
58+
利用x^n * x^n = x^(2n)这个公式。如果我们已知x^(n/2)的话,那么我们可以直接计算出x^n了。那怎么求解x^(n/2)呢,又通过x^(n/4),这不就是递归吗?
59+
*/
60+
#pragma mark - 快速幂递归
61+
//good
62+
double fastPow(double x , long n){
63+
if (n == 0) {
64+
return 1.0;
65+
}
66+
double r = fastPow(x, n/ 2);
67+
if (n % 2 == 0) {
68+
//如果是偶数的话
69+
return r * r;
70+
}else{
71+
return r * r * x;//比如5 = 2 * 2 + 1
72+
}
73+
}
74+
double myPow(double x, int n){
75+
long N = n; //这里用long来保存,因为-n可能会导致数据溢出。
76+
if (N < 0) {
77+
x = 1 / x;
78+
N = -N;
79+
}
80+
return fastPow(x, N);
81+
}
82+
#pragma mark - 快速幂循环
83+
/*
84+
思路:
85+
现在我们要计算x^n。我们把n看成二进制编码,那么N可以利用二进制运算计算得出。比如:
86+
87+
x = 2 , n = 5, 0101
88+
那么 x^n = x^1 * x^4,即当二进制当前位i的值为1时,我们需要乘以x^(2^i).
89+
*/
90+
double myPow3(double x, int n){
91+
long N = n; //这里用long来保存,因为-n可能会导致数据溢出。
92+
if (N < 0) {
93+
x = 1 / x;
94+
N = -N;
95+
}
96+
double result = 1.0;
97+
double ctPositionValue = x;
98+
for (long i=n; i>0; i>>=1) {
99+
if (i & 1 ) {//如果末尾为1的话
100+
result *= ctPositionValue;
101+
}
102+
ctPositionValue*=ctPositionValue;//1,2,4,8,16
103+
}
104+
return result;
105+
}
106+
107+
void test_pow(){
108+
std::cout << myPow3(2.0,INT32_MAX) << std::endl;
109+
std::cout << myPow3(2.0,20) << std::endl;
110+
std::cout << myPow2(2.0,20) << std::endl;
111+
}
112+
}
113+
#endif /* powx_hpp */

0 commit comments

Comments
 (0)