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(, 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的更多相关文章
- [Swift]LeetCode784. 字母大小写全排列 | Letter Case Permutation
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- Leetcode784.Letter Case Permutation字母大小写全排列
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串.返回所有可能得到的字符串集合. 示例: 输入: S = "a1b2" 输出: ["a1 ...
随机推荐
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- html03表单
<!DOCTYPE HTML> <html> <head> <title>用户登录的表单</title> </head> < ...
- LeetCode OJ:Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- C++轮子队-第五周--测试与发布
Alpha版本测试报告 测试找出的BUG 测试结果bug清单: 修复的bug: 按方向下键部分情况无法合并的bug 棋盘图形布局错乱的bug 分数显示不出来的bug 重开游戏无法下坠方块的bug 无法 ...
- WeChat on Web 部分功能模拟实现
Flask from flask import Flask,request,render_template,session,jsonify import time import requests im ...
- vbs 字符串替换
http://blog.csdn.net/flm2003/article/details/7212448 function返回值 http://www.cnblogs.com/wakey/p/5758 ...
- PHP防止mysql注入方法
本文纲要: 1. 初始化你的变量 2. 一定记得要过滤你的变量 [一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /us ...
- linux中文件或者文件夹的基本操作(复制,移动,删除,查找,压缩)
linux 文件(文件夹)的创建,复制,移动,重命名,删除基本命令 复制文件或整个目录 cp 源文件名 目标文件夹/[目标文件名]cp -rv 源文件夹 目标文件夹/[目标文件夹名] --r 递归目录 ...
- freemarker 常见问题
<#setting date_format="yyyy-MM-dd"> ..设置时间格式然后获取从后台获取值${s.createTime?date}这样就能正常显示了 ...
- hexo配置主题发表文章
将本地hexo博客部署到github上了,如果想换我们自己喜欢的主题(默认为landscape),同时也要发表发表文章呢,下面给介绍下: 1,进入hexo官网,预览自己想要的主题,https://gi ...