Skip to content

Commit 1f8121c

Browse files
committedAug 26, 2019
2019-08-25
1 parent cad5291 commit 1f8121c

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed
 

‎0248.中心对称数III/0248-中心对称数III.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@ def strobogrammaticInRange(self, low, high):
88
if int(low) > int(high):
99
return 0
1010
self.findStrobogrammatic(len(high))
11-
#在low里找
11+
1212
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小
2019
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)
2621

2722
if len(low) + 1 == len(high):
2823
return low_cnt + high_cnt
@@ -31,20 +26,9 @@ def strobogrammaticInRange(self, low, high):
3126
else:
3227
tmp = 0
3328
for l in range(len(low) + 1, len(high)):
34-
# print l, self.record
3529
tmp += len(self.record[l])
3630
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+
4832

4933

5034

0 commit comments

Comments
 (0)
Please sign in to comment.