247. Strobogrammatic Number II
Problem description:
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Find all strobogrammatic numbers that are of length = n.
Example:1
2Input: n = 2
Output: ["11","69","88","96"]
Solution:
First, we use some example to find the pattern. If given n= 4.
1 | n = 0: none |
Observe n == 0
and n == 2
, we can see that the latter one is base on the previous one with [1 1], [6 9], [8 8], [9 6] on both side. The same thing happens on n==1
and n==3
.
One thing to notice is that when n == 4
, we can not put 0
on each side.
1 | class Solution { |
time complexity: $O(5^n))$, for every recursive call, there will be 5 different result for every string.
space complexity: $O(n/2))$, stack space