代码拉取完成,页面将自动刷新
# Time: O(5^(n/2))
# Space: O(n)
class Solution(object):
lookup = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}
cache = {}
# @param {string} low
# @param {string} high
# @return {integer}
def strobogrammaticInRange(self, low, high):
count = self.countStrobogrammaticUntil(high, False) - \
self.countStrobogrammaticUntil(low, False) + \
self.isStrobogrammatic(low)
return count if count >= 0 else 0
def countStrobogrammaticUntil(self, num, can_start_with_0):
if can_start_with_0 and num in self.cache:
return self.cache[num]
count = 0
if len(num) == 1:
for c in ['0', '1', '8']:
if num[0] >= c:
count += 1
self.cache[num] = count
return count
for key, val in self.lookup.iteritems():
if can_start_with_0 or key != '0':
if num[0] > key:
if len(num) == 2: # num is like "21"
count += 1
else: # num is like "201"
count += self.countStrobogrammaticUntil('9' * (len(num) - 2), True)
elif num[0] == key:
if len(num) == 2: # num is like 12".
if num[-1] >= val:
count += 1
else:
if num[-1] >= val: # num is like "102".
count += self.countStrobogrammaticUntil(self.getMid(num), True)
elif (self.getMid(num) != '0' * (len(num) - 2)): # num is like "110".
count += self.countStrobogrammaticUntil(self.getMid(num), True) - \
self.isStrobogrammatic(self.getMid(num))
if not can_start_with_0: # Sum up each length.
for i in xrange(len(num) - 1, 0, -1):
count += self.countStrobogrammaticByLength(i)
else:
self.cache[num] = count
return count
def getMid(self, num):
return num[1:len(num) - 1]
def countStrobogrammaticByLength(self, n):
if n == 1:
return 3
elif n == 2:
return 4
elif n == 3:
return 4 * 3
else:
return 5 * self.countStrobogrammaticByLength(n - 2)
def isStrobogrammatic(self, num):
n = len(num)
for i in xrange((n+1) / 2):
if num[n-1-i] not in self.lookup or \
num[i] != self.lookup[num[n-1-i]]:
return False
return True
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。