Skip to content

Commit e8fcd3f

Browse files
committed
添加题35,74,278
1 parent 879fffc commit e8fcd3f

File tree

2 files changed

+101
-10
lines changed

2 files changed

+101
-10
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@
1010

1111
## 核心内容
1212

13-
#### 数据结构篇 🐢
13+
### 数据结构篇 🐰
1414

1515
- [二叉树](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AF%87/%E4%BA%8C%E5%8F%89%E6%A0%91.md)
1616
- [链表](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AF%87/%E9%93%BE%E8%A1%A8.md)
1717

1818
- [栈和队列](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AF%87/%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97.md)
1919
- [二进制](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AF%87/%E4%BA%8C%E8%BF%9B%E5%88%B6.md)
2020

21+
### 基础算法篇 🐮
22+
23+
- [二分搜索](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95%E7%AF%87/%E4%BA%8C%E5%88%86%E6%90%9C%E7%B4%A2.md)
24+
- [排序算法](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95%E7%AF%87/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95.md)
25+
- [动态规划](https://github.com/ligecarryme/algorithm-pattern-JavaScript/blob/master/%E5%9F%BA%E7%A1%80%E7%AE%97%E6%B3%95%E7%AF%87/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92.md)
26+
27+
### 算法思维篇 🦁
28+

基础算法篇/二分搜索.md

+92-9
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
- 3、比较中点和目标值:A[mid] ==、 <、> target
1212
- 4、判断最后两个元素是否符合:A[start]、A[end] ? target
1313

14-
时间复杂度 O(logn),使用场景一般是有序数组的查找
14+
时间复杂度 O(logn),使用场景一般是有序数组的查找
1515

16-
##### 704.二分查找 [binary-search](https://leetcode-cn.com/problems/binary-search/)
16+
##### [704.二分查找(经典示例)](https://leetcode-cn.com/problems/binary-search/)
1717

1818
```js
1919
// 二分搜索最常用模板
@@ -43,28 +43,30 @@ var search = function(nums, target) {
4343

4444
大部分二分查找类的题目都可以用这个模板,然后做一点特殊逻辑即可。
4545

46-
**模板 #1** (left <= right)
46+
**模板 #1** `(left <= right)`
4747

4848
二分查找的最基础和最基本的形式。
4949
查找条件可以在不与元素的两侧进行比较的情况下确定(或使用它周围的特定元素)。
5050
不需要后处理,因为每一步中,你都在检查是否找到了元素。如果到达末尾,则知道未找到该元素。
5151

52-
**模板 #2** (left < right)
52+
**模板 #2** `(left < right)`
5353

5454
一种实现二分查找的高级方法。
5555
查找条件需要访问元素的直接右邻居。
5656
使用元素的右邻居来确定是否满足条件,并决定是向左还是向右。
5757
保证查找空间在每一步中至少有 2 个元素。
5858
需要进行后处理。 当你剩下 1 个元素时,循环 / 递归结束。 需要评估剩余元素是否符合条件。
5959

60-
**模板 #3** (left + 1 < right)
60+
**模板 #3** `(left + 1 < right)`
6161

6262
实现二分查找的另一种方法。
6363
搜索条件需要访问元素的直接左右邻居。
6464
使用元素的邻居来确定它是向右还是向左。
6565
保证查找空间在每个步骤中至少有 3 个元素。
6666
需要进行后处理。 当剩下 2 个元素时,循环 / 递归结束。 需要评估其余元素是否符合条件。
6767

68+
![](https://camo.githubusercontent.com/b3228d92578552d9098924866218c3183e3ae63d66d46a9d156e478329344206/68747470733a2f2f696d672e667569626f6f6d2e636f6d2f696d672f62696e6172795f7365617263685f74656d706c6174652e706e67)
69+
6870
如果是最简单的二分搜索,不需要找第一个、最后一个位置、或者是没有重复元素,可以使用**模板#1**,代码更简洁:
6971

7072
```js
@@ -93,11 +95,9 @@ var search = function(nums, target) {
9395
};
9496
```
9597

96-
##### 34.在排序数组中查找元素的第一个和最后一个位置 [find-first-and-last-position-of-element-in-sorted-array](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
97-
98-
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
98+
##### [34.在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
9999

100-
如果数组中不存在目标值 target,返回 [-1, -1]
100+
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。如果数组中不存在目标值 target,返回 [-1, -1]
101101

102102
**进阶:**
103103

@@ -151,3 +151,86 @@ var searchRange = function(nums, target) {
151151
};
152152
```
153153

154+
##### [35. 搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)
155+
156+
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
157+
158+
```js
159+
var searchInsert = function (nums, target) {
160+
let left = 0, mid = 0, right = nums.length - 1
161+
let res = 0
162+
while (left + 1 < right) {
163+
mid = left + ((right - left) >> 1)
164+
if (nums[mid] < target) {
165+
left = mid
166+
} else {
167+
right = mid
168+
}
169+
}
170+
if (target <= nums[left]) {
171+
return left
172+
} else if (target <= nums[right]) {
173+
return right
174+
} else {
175+
return right + 1
176+
}
177+
};
178+
```
179+
180+
##### [74. 搜索二维矩阵](https://leetcode-cn.com/problems/search-a-2d-matrix/)
181+
182+
编写一个高效的算法来判断 `m x n` 矩阵中,是否存在一个目标值。
183+
184+
```js
185+
var searchMatrix = function (matrix, target) {
186+
let row = matrix.length
187+
let col = matrix[0].length
188+
let start = 0
189+
let end = row * col - 1
190+
let mid = 0
191+
while (start + 1 < end) {
192+
mid = start + ((end - start) >> 1)
193+
val = matrix[parseInt(mid / col)][mid % col]
194+
if (val > target) {
195+
end = mid
196+
} else if (val < target) {
197+
start = mid
198+
} else {
199+
return true
200+
}
201+
}
202+
if (matrix[parseInt(start / col)][start % col] === target || matrix[parseInt(end / col)][end % col] === target) {
203+
return true
204+
}
205+
return false
206+
};
207+
```
208+
209+
##### [278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/)
210+
211+
```js
212+
var solution = function (isBadVersion) {
213+
/**
214+
* @param {integer} n Total versions
215+
* @return {integer} The first bad version
216+
*/
217+
return function (n) {
218+
let start = 1
219+
let end = n
220+
let mid = 1
221+
while (start + 1 < end) {
222+
mid = start + ((end - start) >> 1)
223+
if(isBadVersion(mid)){
224+
end = mid
225+
} else {
226+
start = mid
227+
}
228+
}
229+
if(isBadVersion(start)){
230+
return start
231+
}
232+
return end
233+
};
234+
};
235+
```
236+

0 commit comments

Comments
 (0)