LeetCode(Google) 0247 Strobogrammatic Number 2

proheart

LeetCode 0247 Strobogrammatic Number 2

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"].

public class StrobogrammaticNumberII {  
    public List<String> findStrobogrammatic(int n) {  
        return helper(n, n);  
    }  
  
    private List<String> helper(int n, int m) {  
        if (n == 0) return new ArrayList<>(Arrays.asList(""));  
        if (n == 1) return new ArrayList<>(Arrays.asList("0", "1", "8"));  
        List<String> list = helper(n - 2, m);  
        List<String> result = new ArrayList<>();  
        for (int i = 0; i < list.size(); i++) {  
            String s = list.get(i);  
            if (n != m)     result.add("0" + s + "0");  
            result.add("1" + s + "1");  
            result.add("6" + s + "9");  
            result.add("8" + s + "8");  
            result.add("9" + s + "6");  
        }  
        return result;  
    }  
}
阅读 1.7k

来刷算法
Learn data structure and algorithm using java.

Developer, Java & Android

38 声望
20 粉丝
0 条评论

Developer, Java & Android

38 声望
20 粉丝
文章目录
宣传栏