Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
strobogrammatic-number-iii.py 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-12-30 21:04 +08:00 . remove semicolons
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助