LeetCode 784 Letter Case Permutation 解题报告
题目要求
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.
题目分析及思路
给定一个字符串,考虑每个字母都有大小写,返回可能的所有字符串。由于每个字母都有两种形式,可结合二进制的思想,有n个字母就有2^n种结果。遍历2^n范围内的所有数字,其对应的二进制形式,0代表小写字母,1代表大写字母。再创建一个新字符串并遍历所给字符串的每个字符,若为数字,则直接加到新字符串的后面,若不是,则对应该位置为0,添加字母的小写形式,否则添加大写形式。直到得到所有结果。
python代码
class Solution:
def letterCasePermutation(self, S: str) -> List[str]:
res = []
letters = [c for c in S if c.isalpha()]
l = len(letters)
for i in range(1 << l):
j = 0
s = ''
for c in S:
if c > '9':
if (i >> j) & 1 == 1:
s += c.upper()
else:
s += c.lower()
j += 1
else:
s += c
res.append(s)
return res
LeetCode 784 Letter Case Permutation 解题报告的更多相关文章
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 【Leetcode_easy】784. Letter Case Permutation
problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...
- [LeetCode&Python] Problem 784. Letter Case Permutation
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- 784. Letter Case Permutation 字符串中字母的大小写组合
[抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 784. Letter Case Permutation C++字母大小写全排列
网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...
- 【easy】784. Letter Case Permutation
Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...
随机推荐
- Java编程的逻辑 (84) - 反射
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- python+selenium+unnittest框架
python+selenium+unnittest框架,以百度搜索为例,做了一个简单的框架,先看一下整个项目目录结构 我用的是pycharm工具,我觉得这个工具是天使,超好用也超好看! 这些要感谢原作 ...
- 随笔:JS对象无new构造原理
var myFun = function(words) { if (!(this instanceof myFun)) { return new myFun(words); } this.name = ...
- python实现微信接口——itchat模块
python实现微信接口——itchat模块 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方 ...
- webpack 运行提示“The ‘mode‘ option has not been set”的原因和解决方法
最近在研究webpack,当我执行npm run build / npm start / npm run server等命令时,都是提示下面的警告信息 WARNING in configuration ...
- 【转载】Eclipse快捷键 10个最有用的快捷键
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- 解决Maven build 慢的问题
extends:http://www.cnblogs.com/gmq-sh/p/4742698.html ,http://www.cnblogs.com/rainy-shurun/p/5726758. ...
- smarty模板操作变量
smarty模板技术分配变量的细节问题. 从php中获取数据 一句话:可以分配php支持的各种数据类型. php: 基本数据类型 int double string bool ...
- MFC从资源加载文本
bool CWizardSheet::GetTextResource(UINT uID, CString& csContent) { HMODULE hModule=GetModuleHand ...
- B - Space Ant
The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists trace ...