Skip to content

Commit

Permalink
2019-08-25
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Aug 26, 2019
1 parent cad5291 commit 1f8121c
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions 0248.中心对称数III/0248-中心对称数III.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ def strobogrammaticInRange(self, low, high):
if int(low) > int(high):
return 0
self.findStrobogrammatic(len(high))
#在low里找

low_rec = self.record[len(low)]
#找第一个 >= low的数的下标
low_cnt = 0
for i, num in enumerate(low_rec):
if int(num) >= int(low):
low_cnt = len(low_rec) - i
break

#找在low_rec里有多少个数 >= low,结果放在low_cnt里
#这里相当于在找左侧边界

low_cnt = len(low_rec) - bisect.bisect_left(low_rec, low)

#找第一个 > high的数的下标,如果没找到,则说明这个数组里所有的数都比 high小
high_rec = self.record[len(high)]
high_cnt = len(high_rec)
for i, num in enumerate(high_rec):
if int(num) > int(high):
high_cnt = i
break
high_cnt = bisect.bisect_right(high_rec, high)

if len(low) + 1 == len(high):
return low_cnt + high_cnt
Expand All @@ -31,20 +26,9 @@ def strobogrammaticInRange(self, low, high):
else:
tmp = 0
for l in range(len(low) + 1, len(high)):
# print l, self.record
tmp += len(self.record[l])
return tmp + low_cnt + high_cnt
#找第一个 > high的数的下标
# left, right = 0, len(low_rec) - 1
# while left < right:
# mid = (left + right) // 2
# if low_rec[mid] == low:
# low_cnt = len(low_rec) - mid
# elif low_rec[mid] > low:
# right = mid - 1
# elif low_rec[mid] < low:
# left = mid + 1





Expand Down

0 comments on commit 1f8121c

Please sign in to comment.