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", ...
随机推荐
- 【Unity】序列化字典Dictionary的问题
问题:在C#脚本定义了public Dictionary字典,然而在编辑器检视面板Editor Inspector中看不到(即无法序列化字典).即不能在编辑器中拖拽给字典赋值. 目标:检视面板Insp ...
- Android自动化测试之Monkeyrunner从零开始
最近由于公司在组织一个Free CoDE的项目,也就是由大家自己选择研究方向来做一些自己感兴趣的研究.由于之前我学过一点点关于android的东西,并且目前android开发方兴未艾如火如荼,但自动化 ...
- Swing中支持自动换行的WrapLayout
http://www.cnblogs.com/TLightSky/p/3482454.html ———————————————————————————————————————————————————— ...
- Sphinx 2.2.11-release reference manual
1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...
- Java知多少(94)键盘事件
键盘事件的事件源一般丐组件相关,当一个组件处于激活状态时,按下.释放或敲击键盘上的某个键时就会发生键盘事件.键盘事件的接口是KeyListener,注册键盘事件监视器的方法是addKeyListene ...
- WebRTC 配置环境
复制文件到指定文件路径 cp -rf /home/leehongee/LeeHonGee/jdk1.7.0_45 /usr/lib/jvm 创建文件夹 mkdir jvm 修改环境变量 sudo ...
- spark2.2jdbc写入mysql 的两种方法(append,Overriedwrite)-不用Mysql建表
import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.{SQLContext, SaveMode} ...
- SpringBoot(十四)-- 整合Swagger2
1.pom依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
- 构建工具:grunt、Glup、webpack
相关代码已上传至github 怎么是项目构建? 编译项目中的js, sass, less: 合并js/css等资源文件: 压缩js/css/html等资源文件: JS语法的检查. 构建工具的作用? 简 ...
- VS2017 配置freeglut3.0.0
配置freeglut: 1. 先下载cmake和freeglut3.0.0文件,并使用cmake编译freeglut,再使用vs2017生成解决方案,详细步骤见https://blog.csdn.ne ...