• Leetcode: Strobogrammatic Number II


    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 }
  • 相关阅读:
    [JavaScript]JS由来
    [HTML5]HTML表单(Forms)
    Linux 配置SSH 无密钥登陆
    Spring Boot 的 application.properties
    Spring Boot 全局异常捕获
    Linux hostname设置,静态ip设置,hostname与静态ip相互映射
    Hadoop 集群的三种方式
    网站列表
    Hadoop -- 概念
    shell 编程
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5062486.html
Copyright © 2020-2023  润新知