forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37.py
49 lines (39 loc) · 1.13 KB
/
37.py
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
41
42
43
44
45
46
47
48
49
# -*- coding:utf-8 -*-
class Solution:
def GetNumberOfK(self, data, k):
# write code here
size = len(data)
if size == 0:
return 0
if k > data[-1] or k < data[0]:
return 0
count = 0
startIndex = 0
endIndex = size - 1
index = size - 1
while True:
if index < 0 or index > size:
break
if data[index] > k:
endIndex = index
index = int((startIndex + endIndex) / 2)
elif data[index] > k:
startIndex = index
index = int((startIndex + endIndex) / 2)
else:
for i in range(index, -1, -1):
if data[i] == k:
count += 1
else:
break
for i in range(index + 1, size):
if data[i] == k:
count += 1
else:
break
break
return count
a = [1, 2, 3, 4, 5, 5]
s = Solution()
count = s.GetNumberOfK(a, 5)
print count