• leetcode784


    这道题经过独立思考,通过使用二进制编码的方式来进行处理。分几个步骤一层一层的处理,最终解决了,这道题感觉应该属于medimu级别。

    public class Solution
        {
            /// <summary>
            /// 列出二进制表示
            /// </summary>
            /// <param name="count"></param>
            /// <returns></returns>
            public IList<string> GetBinaryPartten(int count)
            {
                var list = new List<string>();
                //int count = 5;//字母数量
                int N = (int)Math.Pow(2, count) - 1;//最大值
    
                for (int i = 0; i <= N; i++)
                {
                    Stack<int> St = new Stack<int>();
                    int x = i;//1010
                    while (x != 0)
                    {
                        int y = x & 1;
                        St.Push(y);
                        x >>= 1;
                    }
    
                    int len = St.Count;//len<=count
                    string result = "";
                    for (int j = 0; j < count - len; j++)
                    {
                        result += "0";
                    }
    
                    while (St.Any())
                    {
                        int y = St.Pop();
                        result += y.ToString();
                    }
    
                    list.Add(result);
                }
                return list;
            }
    
            public IList<string> LetterCasePermutation(string S)
            {
                S = S.ToLower();
                var dic = new List<KeyValuePair<int, string>>();
                var list = new List<string>();
                var numdic = new HashSet<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                int count = 0;
                for (int i = 0; i < S.Length; i++)
                {
                    if (numdic.Any(x => x == S[i]))//是数字
                    {
                        continue;
                    }
                    else//是字母
                    {
                        count++;//记录字母的数量
                        dic.Add(new KeyValuePair<int, string>(i, S[i].ToString()));//记录字母的下标和字母
                    }
                }
                var list0 = GetBinaryPartten(count);
                foreach (var l0 in list0)
                {
                    string temp = S;//原格式
                    string map = l0;//当前格式编码
                    //解析编码
                    for (int i = 0; i < map.Length; i++)
                    {
                        //按位解析,i表示dic[i]
                        var code = map[i];//code表示编码
    
                        var d = dic[i];//根据当前位的编码,解析出字符
                        var position = d.Key;//原串中的下标
                        var chars = d.Value;//当前字符
    
                        if (code == '0')
                        {
                            chars = chars.ToLower();
                        }
                        else
                        {
                            chars = chars.ToUpper();
                        }
                        //对当前位进行处理
                        string prefix = temp.Substring(0, position);
                        //string mid = temp.Substring(position, 1);
                        string mid = chars;//替换此位字符形式
                        string next = temp.Substring(position + 1);
                        temp = prefix + mid + next;
                    }
                    //这里将此格式的字符串加入list中
                    list.Add(temp);
                }
                return list;
            }
        }

     另一种解法,使用回溯法:

     public class Solution
        {
            List<string> list = new List<string>();
            private void BackTrack(int t, string S)
            {
                if (t >= S.Length)
                {
                    return;
                }
                for (int i = t; i < S.Length; i++)
                {
                    var s = S[i];
                    if (s >= 48 && s <= 57)
                    {
                        continue;//数字0到9
                    }
                    else//字母
                    {                    
                        string prefix = S.Substring(0, i);
                        //string mid = temp.Substring(position, 1);
                        string mid = s.ToString().ToUpper();//替换此位字符形式
                        string next = S.Substring(i + 1);
                        string temp = prefix + mid + next;
                        list.Add(temp);
                        BackTrack(i + 1, temp);
                    }
                }
            }
    
            public IList<string> LetterCasePermutation(string S)
            {
                var numdic = new HashSet<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                S = S.ToLower();
                list.Add(S);
                BackTrack(0, S);
                return list;
            }
        }

     再提供一种回溯法的思路,使用python实现:

     1 class Solution:    
     2     def BackTrack(self,S,l,index,string):
     3         if index < len(S):
     4             c = S[index]
     5             if c.isalpha():
     6                 self.BackTrack(S,l,index+1,string+c.lower())
     7                 self.BackTrack(S,l,index+1,string+c.upper())
     8             else:
     9                 self.BackTrack(S,l,index+1,string+c)
    10         if index == len(S):
    11             l.append(string)
    12 
    13         return
    14 
    15     def letterCasePermutation(self, S: 'str') -> 'List[str]':
    16         l = list()
    17         n = len(S)
    18         tags = [0] * n
    19         self.BackTrack(S,l,0,'')
    20         return l
  • 相关阅读:
    css实现梯形
    CSS3自定义滚动条样式 -webkit-scrollbar
    MySQL-5.6.13免安装版配置方法
    CSS边框长度控制
    The Shapes of CSS(css的形状)
    CSS制作图形速查表
    使用onclick跳转到其他页面/跳转到指定url
    TCP/IP协议
    jQuery实现全选、反选和不选功能
    checkbox在vue中的用法小结
  • 原文地址:https://www.cnblogs.com/asenyang/p/9726834.html
Copyright © 2020-2023  润新知