Created
April 13, 2016 20:52
-
-
Save cangoal/7fc052bf0248c928efd9e3ef89ca86ef to your computer and use it in GitHub Desktop.
LeetCode - Strobogrammatic Number II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 List<String> findStrobogrammatic(int n) { | |
| return helper(n, n); | |
| } | |
| private List<String> helper(int n, int len){ | |
| List<String> res = new ArrayList<String>(); | |
| if(n <= 0) { | |
| res.add(""); | |
| return res; | |
| } | |
| if(n == 1){ | |
| res.add("0"); | |
| res.add("1"); | |
| res.add("8"); | |
| return res; | |
| } | |
| List<String> lst = helper(n - 2, len); | |
| for(String str : lst){ | |
| res.add("1" + str + "1"); | |
| res.add("6" + str + "9"); | |
| res.add("8" + str + "8"); | |
| res.add("9" + str + "6"); | |
| if(len != n) res.add("0" + str + "0"); | |
| } | |
| return res; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment