247. Strobogrammatic Number II

247. Strobogrammatic Number II

http://www.cnblogs.com/grandyang/p/5200919.html

https://leetcode.com/problems/strobogrammatic-number-ii/discuss/67280/AC-clean-Java-solution

这道题是之前那道Strobogrammatic Number的拓展,那道题让我们判断一个数是否是对称数,而这道题让我们找出长度为n的所有的对称数,我们肯定不能一个数一个数的来判断,那样太不高效了,而且OJ肯定也不会答应。题目中给了提示说可以用递归来做,而且提示了递归调用n-2,而不是n-1。我们先来列举一下n为0,1,2,3,4的情况:

n = 0:   none
n = 1:   0, 1, 8
n = 2:   11, 69, 88, 96
n = 3:   101, 609, 808, 906, 111, 619, 818, 916, 181, 689, 888, 986
n = 4:   1001, 6009, 8008, 9006, 1111, 6119, 8118, 9116, 1691, 6699, 8698, 9696, 1881, 6889, 8888, 9886, 1961, 6969, 8968, 9966


我们注意观察n=0和n=2,可以发现后者是在前者的基础上,每个数字的左右增加了[1 1], [6 9], [8 8], [9 6],看n=1和n=3更加明显,在0的左右增加[1 1],变成了101, 在0的左右增加[6 9],变成了609, 在0的左右增加[8 8],变成了808, 在0的左右增加[9 6],变成了906, 然后在分别在1和8的左右两边加那四组数,我们实际上是从m=0层开始,一层一层往上加的,需要注意的是当加到了n层的时候,左右两边不能加[0 0],因为0不能出现在两位数及多位数的开头,在中间递归的过程中,需要有在数字左右两边各加上0的那种情况,参见代码如下:  
 

Arrays.asList returns a fixed sized list





class Solution {
    public List<String> findStrobogrammatic(int n) {
      return helper(n, n); // remember to return 
        
    }
  
    private List<String> helper(int n, int m){
      // base case
      // if(n == 0){
      //   return new ArrayList<>("");
      // }
      // if(n == 1){
      //   return new ArrayList<>("1","8", "0");
      // }
      if (n == 0){
        return new ArrayList<String>(Arrays.asList(""));
        // Arrays.asList returns a fixed sized list
      }
      if (n == 1){
        return new ArrayList<String>(Arrays.asList("1","0","8"));
      }
      
      List<String> prevResult = helper(n - 2, m);
      
      List<String> result = new ArrayList<>();
      
      // two cases: if we are filling numbers on the very outside, we can add 1 6 8 9 
      // another case is if we are not on the very outside, we can add 1 6 8 9 0 
      
      for(String s : prevResult){
        if(n != m){
          // we are not on the very outside, which means we can fill up with 0 
          result.add("0" + s + "0");
        }
        
        result.add("1" + s + "1");
        result.add("6" + s + "9");
        result.add("9" + s + "6");
        result.add("8" + s + "8");
      }
      return result;
    }
}

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9455054.html