247. Strobogrammatic Number II

My Submissions

Total Accepted: 10689
Total Submissions: 30073
Difficulty: Medium

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.

Hide Company Tags
Google
Hide Tags
Math Recursion
Hide Similar Problems
(E) Strobogrammatic Number (H) Strobogrammatic Number III

public class Solution {
    // https://discuss.leetcode.com/topic/20753/ac-clean-java-solution
    public List<String> findStrobogrammatic(int n) {
        return helper(n, n);
    }
    
    private List<String> helper(int n, int m) {
        if (n == 0) return new ArrayList<String>(Arrays.asList(""));
        if (n == 1) return new ArrayList<String>(Arrays.asList("0", "1", "8"));
        
        List<String> list = helper(n - 2, m);
        
        List<String> res = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (n != m) res.add("0" + s + "0");
              
            res.add("1" + s + "1");
            res.add("6" + s + "9");
            res.add("8" + s + "8");
            res.add("9" + s + "6");
        }
        return res;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • A strobogrammatic number is a number that looks the same ...
    Jeanz阅读 309评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • 肉松先生和茄子小姐是在一家快餐店认识的,肉松每天下班都会坐在快餐店边吃边打游戏,他开始注意到茄子是因为她的哭声持续...
    码字赚钱阅读 455评论 0 1
  • 许多酒店、餐厅、饭馆,它们的背后都有某些支持。小到不起眼的饭馆可以包下了今年夏天附近施工队的午餐,再到某个半红不紫...
    黄陈阅读 144评论 0 0
  • ❀万进 | 文、摄影(此文曾刊发于《北京九三社讯》副刊) (1) 老实说,对祁连山没概念。 想当然以为那是蛮荒之地...
    万进随笔阅读 448评论 0 1