@@ -8,21 +8,16 @@ def strobogrammaticInRange(self, low, high):
8
8
if int (low ) > int (high ):
9
9
return 0
10
10
self .findStrobogrammatic (len (high ))
11
- #在low里找
11
+
12
12
low_rec = self .record [len (low )]
13
- #找第一个 >= low的数的下标
14
- low_cnt = 0
15
- for i , num in enumerate (low_rec ):
16
- if int (num ) >= int (low ):
17
- low_cnt = len (low_rec ) - i
18
- break
19
-
13
+ #找在low_rec里有多少个数 >= low,结果放在low_cnt里
14
+ #这里相当于在找左侧边界
15
+
16
+ low_cnt = len (low_rec ) - bisect .bisect_left (low_rec , low )
17
+
18
+ #找第一个 > high的数的下标,如果没找到,则说明这个数组里所有的数都比 high小
20
19
high_rec = self .record [len (high )]
21
- high_cnt = len (high_rec )
22
- for i , num in enumerate (high_rec ):
23
- if int (num ) > int (high ):
24
- high_cnt = i
25
- break
20
+ high_cnt = bisect .bisect_right (high_rec , high )
26
21
27
22
if len (low ) + 1 == len (high ):
28
23
return low_cnt + high_cnt
@@ -31,20 +26,9 @@ def strobogrammaticInRange(self, low, high):
31
26
else :
32
27
tmp = 0
33
28
for l in range (len (low ) + 1 , len (high )):
34
- # print l, self.record
35
29
tmp += len (self .record [l ])
36
30
return tmp + low_cnt + high_cnt
37
- #找第一个 > high的数的下标
38
- # left, right = 0, len(low_rec) - 1
39
- # while left < right:
40
- # mid = (left + right) // 2
41
- # if low_rec[mid] == low:
42
- # low_cnt = len(low_rec) - mid
43
- # elif low_rec[mid] > low:
44
- # right = mid - 1
45
- # elif low_rec[mid] < low:
46
- # left = mid + 1
47
-
31
+
48
32
49
33
50
34
0 commit comments