248. Strobogrammatic Number III:
其实抽丝剥茧,这题就是一道recursion的题目,先去不停的构建在长度范围内的值,然后选取合适的值count一下
class Solution(object):
def strobogrammaticInRange(self, low, high):
"""
:type low: str
:type high: str
:rtype: int
"""
count = 0
rst = []
for i in range(len(low), len(high)+1):
rst += self.helper(i)
for num in rst:
if (len(num) == len(low) and num < low) or (len(num) == len(high) and num > high) or (num[0] == "0" and num != "0"):
continue
count += 1
return count
def helper(self, cur):
if cur <= 0:
return [""]
if cur == 1:
return ["1","8","0"]
rst = []
center = self.helper(cur-2);
for i in range(len(center)):
tmp = center[i]
rst.append("0" + tmp + "0")
rst.append("1" + tmp + "1")
rst.append("6" + tmp + "9")
rst.append("8" + tmp + "8")
rst.append("9" + tmp + "6")
return rst