8.15 - hard - 47

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容