码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 247: Strobogrammatic Number II

时间:2017-12-18 14:24:04      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:remove   --   find   continue   sid   numbers   start   art   lca   

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

 

 1 public class Solution {
 2     public IList<string> FindStrobogrammatic(int n) {
 3         return DFS(n, n);
 4     }
 5     
 6     private IList<string> DFS(int n, int m)
 7     {
 8         if (n == 0)
 9         {
10             return new List<string>() {""};
11         }
12         
13         if (n == 1)
14         {
15             return new List<string>() {"0", "1", "8"};
16         }
17         
18         var list = DFS(n - 2, m);
19         var result = new List<string>();
20         
21         foreach (var s in list)
22         {
23             if (n != m)
24             {
25                 result.Add("0" + s + "0");
26             }
27             
28             result.Add("1" + s + "1");
29             result.Add("9" + s + "6");
30             result.Add("8" + s + "8");
31             result.Add("6" + s + "9");
32         }
33         
34         return result;
35     }
36 }
37 
38 // backtracking, it didn‘t pass large test case
39 public class Solution1 {
40     private int[] allCandidates = new int[] {0, 1, 6, 8, 9};
41     private int[] centerCandidates = new int[] {0, 1, 8};
42     private Dictionary<int, int> map = new Dictionary<int, int>();
43  
44     public IList<string> FindStrobogrammatic(int n) {
45         map[0] = 0;
46         map[1] = 1;
47         map[6] = 9;
48         map[8] = 8;
49         map[9] = 6;
50         
51         var result = new List<string>();
52         DFS(n, 0, new List<int>(), result);
53         return result;
54     }
55     
56     private void DFS(int n, int start, IList<int> res, IList<string> result)
57     {
58         if (start > n / 2)
59         {
60             var sb = new StringBuilder();
61             
62             for (int i = 0; i < res.Count; i++)
63             {
64                 sb.Append(res[i]);
65             }
66             
67             int s = n % 2 == 0 ? res.Count - 1 : res.Count - 2;
68             for (int i = s; i >= 0; i--)
69             {
70                 sb.Append(map[res[i]]);
71             }
72             
73             result.Add(sb.ToString());
74             
75             return;
76         }
77             
78         if (start == n / 2 && n % 2 != 0)
79         {
80             for (int i = 0; i < centerCandidates.Length; i++)
81             {
82                 res.Add(centerCandidates[i]);
83                 DFS(n, start + 1, res, result);
84                 res.RemoveAt(res.Count - 1);
85             }
86         }
87         else
88         {
89             for (int i = 0; i < allCandidates.Length; i++)
90             {
91                 if (start == 0 && allCandidates[i] == 0) continue;
92                 
93                 res.Add(allCandidates[i]);
94                 DFS(n, start + 1, res, result);
95                 res.RemoveAt(res.Count - 1);
96             }
97         }
98     }
99 }

 

Leetcode 247: Strobogrammatic Number II

标签:remove   --   find   continue   sid   numbers   start   art   lca   

原文地址:http://www.cnblogs.com/liangmou/p/8057024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!