diff --git a/docs/Leetcode_Solutions/Python/0247._Strobogrammatic_Number_II.md b/docs/Leetcode_Solutions/Python/0247._Strobogrammatic_Number_II.md index 19d66e24c920b7a17533634a82d64d0c4bc84734..a8f85ee134106eabf428c6f123650dde972f2a4b 100644 --- a/docs/Leetcode_Solutions/Python/0247._Strobogrammatic_Number_II.md +++ b/docs/Leetcode_Solutions/Python/0247._Strobogrammatic_Number_II.md @@ -45,12 +45,7 @@ Output: ["11","69","88","96"] ```python class Solution: - def findStrobogrammatic(self, n): - """ - :type n: int - :rtype: List[str] - """ - + def findStrobogrammatic(self, n: int) -> List[str]: def helper(n): if n == 0: return [''] @@ -66,11 +61,11 @@ class Solution: res.append('9' + s + '6') return res - if n == 0: - return [''] - if n == 1: - return ['0', '1', '8'] - return [s for s in helper(n) if s[0] != '0'] + res = [] + for s in helper(n): + if not (len(s) > 1 and s[0] == '0'): + res.append(s) + return res ``` > 思路 2 @@ -80,26 +75,23 @@ class Solution: ```python class Solution: - def findStrobogrammatic(self, n): - """ - :type n: int - :rtype: List[str] - """ - res = [''] if n % 2 == 0 else ['0', '1', '8'] + def findStrobogrammatic(self, n: int) -> List[str]: + stack = [''] if n % 2 == 0 else ['0', '1', '8'] for i in range(n%2+1, n, 2): cur = [] - for s in res: + for s in stack: cur.append('0' + s + '0') cur.append('1' + s + '1') cur.append('6' + s + '9') cur.append('8' + s + '8') cur.append('9' + s + '6') - res = cur[:] - if n == 0: - return [''] - if n == 1: - return ['0', '1', '8'] - return [s for s in res if s[0] != '0'] + stack = cur[:] + + res = [] + for s in stack: + if not (len(s) > 1 and s[0] == '0'): + res.append(s) + return res ```