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. For example, Given n = 2, return ["11","69","88","96"]. Hint: Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.
Watch out for cornor case:
when n = 2, there shouldn't be "00" returned.
but when n = 4, which uses n =2 as recursion base, "1001" is an answer
1 class Solution { 2 public List<String> findStrobogrammatic(int n) { 3 return helper(n, n); 4 } 5 6 public List<String> helper(int n, int limit) { 7 if (n <= 0) return Arrays.asList(""); 8 if (n == 1) return Arrays.asList("0", "1", "8"); 9 10 List<String> list = helper(n - 2, limit); 11 12 List<String> res = new ArrayList<>(); 13 14 for (String str : list) { 15 if (n != limit) { 16 res.add("0" + str + "0"); 17 } 18 res.add("1" + str + "1"); 19 res.add("6" + str + "9"); 20 res.add("9" + str + "6"); 21 res.add("8" + str + "8"); 22 } 23 return res; 24 } 25 }