这道题经过独立思考,通过使用二进制编码的方式来进行处理。分几个步骤一层一层的处理,最终解决了,这道题感觉应该属于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(, count) - ;//最大值 for (int i = ; i <= N; i++)
{
Stack<int> St = new Stack<int>();
int x = i;//
while (x != )
{
int y = x & ;
St.Push(y);
x >>= ;
} int len = St.Count;//len<=count
string result = "";
for (int j = ; j < count - len; j++)
{
result += "";
} 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> { '', '', '', '', '', '', '', '', '', '' };
int count = ;
for (int i = ; 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 = ; 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 == '')
{
chars = chars.ToLower();
}
else
{
chars = chars.ToUpper();
}
//对当前位进行处理
string prefix = temp.Substring(, position);
//string mid = temp.Substring(position, 1);
string mid = chars;//替换此位字符形式
string next = temp.Substring(position + );
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 >= && s <= )
{
continue;//数字0到9
}
else//字母
{
string prefix = S.Substring(, i);
//string mid = temp.Substring(position, 1);
string mid = s.ToString().ToUpper();//替换此位字符形式
string next = S.Substring(i + );
string temp = prefix + mid + next;
list.Add(temp);
BackTrack(i + , temp);
}
}
} public IList<string> LetterCasePermutation(string S)
{
var numdic = new HashSet<char> { '', '', '', '', '', '', '', '', '', '' };
S = S.ToLower();
list.Add(S);
BackTrack(, S);
return list;
}
}

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

 class Solution:
def BackTrack(self,S,l,index,string):
if index < len(S):
c = S[index]
if c.isalpha():
self.BackTrack(S,l,index+1,string+c.lower())
self.BackTrack(S,l,index+1,string+c.upper())
else:
self.BackTrack(S,l,index+1,string+c)
if index == len(S):
l.append(string) return def letterCasePermutation(self, S: 'str') -> 'List[str]':
l = list()
n = len(S)
tags = [0] * n
self.BackTrack(S,l,0,'')
return l

leetcode784的更多相关文章

  1. [Swift]LeetCode784. 字母大小写全排列 | Letter Case Permutation

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  2. Leetcode784.Letter Case Permutation字母大小写全排列

    给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串.返回所有可能得到的字符串集合. 示例: 输入: S = "a1b2" 输出: ["a1 ...

随机推荐

  1. angular之 $watch 和$digest 和$apply和$observe的区别

    $watch 代表的就是对数据源的监听,当数据源发生变化,就会触发第二个参数的回调函数 $digest 代表触发一个数据源变化的事件 $apply 代表对于$digest的一个封装,他多了一个参数 . ...

  2. WinForm 创建与读写配置文件

    (转自:http://www.cnblogs.com/SkySoot/archive/2012/02/08/2342941.html) 1. 创建 app.config 文件: 右击项目名称,选择“添 ...

  3. C/C++开发android应用

    (转自:http://blog.csdn.net/srplab1/article/details/7617963) 在某些情况下,比如原来与很多c/c++的代码, 可能希望采用c/c++编写andro ...

  4. 多种方式实现滑动p91--105

    1.layout方法 2.offsetLeftAndRight()与offsetTopAndBottom()方法 3.LayoutParams(前提是要有父布局,根据父布局的类型决定LayoutPar ...

  5. ASP.NET学习路线图(转)

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...

  6. GeoWebCache的配置与使用

    最近在做一个开源GIS的demo的工作,工作中涉及到了地图瓦片,选取的开发环境是geoserver+openlayers,那么地图瓦片自然而然也就使用geowebcache,geowebcache就相 ...

  7. 9.proc目录下的文件和目录详解

    1./proc目录下的文件和目录详解 /proc:虚拟目录.是内存的映射,内核和进程的虚拟文件系统目录,每个进程会生成1个pid,而每个进程都有1个目录. /proc/Version:内核版本 /pr ...

  8. MySQL 进入 导入

    命令行进入时 不能用 ‘;’ 结尾

  9. 解决pip安装太慢的问题

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  10. 在Wifi网络中嗅探明文密码(HTTP POST请求、POP等)

    全世界,现在大约50%的网站没有使用SSL加密,天朝尤其多. 我们都知道通过HTTP发送的数据都是明文,没有使用任何加密,即使是在数据敏感的登录页面. 本文的目的是:如果你在不熟悉的网络环境中,要注意 ...