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

leetcode1087

时间:2019-06-16 00:30:55      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:git   isnull   ati   insert   next   for   letter   bin   ring   

 1 public class Solution
 2     {
 3 
 4         public string[] Permute(string S)
 5         {
 6             List<List<char>> dic = new List<List<char>>();
 7             int baseindex = 0;
 8             var temp = S;
 9             var mask = "";
10             while (temp.IndexOf({) >= 0)
11             {
12                 int begin = temp.IndexOf({);
13                 int end = temp.IndexOf(});
14                 var ary = new List<char>();
15                 for (int i = begin + 1; i < end; i++)
16                 {
17                     if (temp[i] != ,)
18                     {
19                         ary.Add(temp[i]);
20                     }
21                 }
22                 dic.Add(ary);
23                 mask = mask + temp.Substring(0, begin) + "#";
24                 baseindex = end + 1;
25                 temp = temp.Substring(baseindex);
26             }
27             mask += S.Substring(S.LastIndexOf(}) + 1);
28 
29             var result = LetterCombinations(S, mask, dic);
30             return result.OrderBy(x => x).ToArray();
31         }
32 
33         public IList<string> LetterCombinations(string S, string mask, List<List<char>> dic)
34         {
35             var combinations = new List<string>();
36 
37             if (string.IsNullOrEmpty(S))
38                 return combinations;
39 
40             combinations.Add("");
41             foreach (var digits in dic)
42             {
43                 var next = new List<string>();
44 
45                 foreach (char letter in digits)
46                 {
47                     foreach (string combo in combinations)
48                     {
49                         var dstr = combo + letter;
50                         next.Add(dstr);
51                     }
52                 }
53                 combinations = next;
54             }
55 
56             for (int i = 0; i < combinations.Count; i++)
57             {
58                 var cur = combinations[i];
59                 for (var j = 0; j < mask.Length; j++)
60                 {
61                     if (mask[j] != #)
62                     {
63                         cur = cur.Insert(j, mask[j].ToString());
64                     }
65                 }
66                 combinations[i] = cur;
67             }
68 
69             return combinations;
70         }
71     }

 

leetcode1087

标签:git   isnull   ati   insert   next   for   letter   bin   ring   

原文地址:https://www.cnblogs.com/asenyang/p/11029375.html

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