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 176fed5

Browse files
Update 034._Search for a Range.md
1 parent 8f177eb commit 176fed5

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

‎docs/Leetcode_Solutions/034._Search for a Range.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
### 34. Search for a Range
1+
# 34. Find First and Last Position of Element in Sorted Array
22

3+
**<font color=red>难度: Medium</font>**
34

5+
## 刷题内容
46

5-
题目:
7+
> 原题连接
68
7-
https://leetcode.com/problems/search-for-a-range/
9+
* https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
810

11+
> 内容描述
912
13+
```
14+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
15+
16+
Your algorithm's runtime complexity must be in the order of O(log n).
17+
18+
If the target is not found in the array, return [-1, -1].
1019
11-
难度 : Medium
20+
Example 1:
1221
22+
Input: nums = [5,7,7,8,8,10], target = 8
23+
Output: [3,4]
24+
Example 2:
25+
26+
Input: nums = [5,7,7,8,8,10], target = 6
27+
Output: [-1,-1]
28+
```
1329

30+
## 解题方案
1431

15-
思路:
32+
> 思路 1
33+
******- 时间复杂度: O(lgN)******- 空间复杂度: O(1)******
1634

1735
二分法,先找```target```出现的左边界,判断是否有```target```后再判断右边界
1836

@@ -30,9 +48,6 @@
3048

3149
AC 代码
3250

33-
34-
35-
3651
```python
3752
class Solution(object):
3853
def searchRange(self, nums, target):
@@ -41,7 +56,8 @@ class Solution(object):
4156
:type target: int
4257
:rtype: List[int]
4358
"""
44-
if not nums : return [-1, -1]
59+
if not nums or len(nums) == 0:
60+
return [-1, -1]
4561

4662
res = []
4763
l, r = 0, len(nums)-1
@@ -57,6 +73,7 @@ class Solution(object):
5773
r = mid - 1
5874
if not res:
5975
return [-1, -1]
76+
6077
# search for right bound
6178
r = len(nums)-1
6279
while l <= r:

0 commit comments

Comments
(0)

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