-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSearchForARange.py
More file actions
executable file
路40 lines (35 loc) 路 966 Bytes
/
Copy pathSearchForARange.py
File metadata and controls
executable file
路40 lines (35 loc) 路 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: UTF-8 -*-
#
# Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
#
# Your algorithm's runtime complexity must be in the order of O(log n).
#
# If the target is not found in the array, return [-1, -1].
#
# For example,
# Given [5, 7, 7, 8, 8, 10] and target value 8,
# return [3, 4].
#
# Python, Python 3 all accepted.
class SearchForARange:
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
result = [-1, -1]
length = len(nums)
if length <= 0:
return result
for i in range(length):
if nums[i] == target:
result[0] = i
break
j = length - 1
while j >= 0:
if nums[j] == target:
result[1] = j
break
j -= 1
return result