未验证 提交 69050b88 编写于 作者: K Keqi Huang 提交者: GitHub

Update 0247._Strobogrammatic_Number_II.md

上级 6e803eb4
...@@ -45,12 +45,7 @@ Output: ["11","69","88","96"] ...@@ -45,12 +45,7 @@ Output: ["11","69","88","96"]
```python ```python
class Solution: class Solution:
def findStrobogrammatic(self, n): def findStrobogrammatic(self, n: int) -> List[str]:
"""
:type n: int
:rtype: List[str]
"""
def helper(n): def helper(n):
if n == 0: if n == 0:
return [''] return ['']
...@@ -66,11 +61,11 @@ class Solution: ...@@ -66,11 +61,11 @@ class Solution:
res.append('9' + s + '6') res.append('9' + s + '6')
return res return res
if n == 0: res = []
return [''] for s in helper(n):
if n == 1: if not (len(s) > 1 and s[0] == '0'):
return ['0', '1', '8'] res.append(s)
return [s for s in helper(n) if s[0] != '0'] return res
``` ```
> 思路 2 > 思路 2
...@@ -80,26 +75,23 @@ class Solution: ...@@ -80,26 +75,23 @@ class Solution:
```python ```python
class Solution: class Solution:
def findStrobogrammatic(self, n): def findStrobogrammatic(self, n: int) -> List[str]:
""" stack = [''] if n % 2 == 0 else ['0', '1', '8']
:type n: int
:rtype: List[str]
"""
res = [''] if n % 2 == 0 else ['0', '1', '8']
for i in range(n%2+1, n, 2): for i in range(n%2+1, n, 2):
cur = [] cur = []
for s in res: for s in stack:
cur.append('0' + s + '0') cur.append('0' + s + '0')
cur.append('1' + s + '1') cur.append('1' + s + '1')
cur.append('6' + s + '9') cur.append('6' + s + '9')
cur.append('8' + s + '8') cur.append('8' + s + '8')
cur.append('9' + s + '6') cur.append('9' + s + '6')
res = cur[:] stack = cur[:]
if n == 0:
return [''] res = []
if n == 1: for s in stack:
return ['0', '1', '8'] if not (len(s) > 1 and s[0] == '0'):
return [s for s in res if s[0] != '0'] res.append(s)
return res
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册