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", ...
随机推荐
- docker启动centos7后sudo不能使用
docker启动centos7后sudo不能使用 过程 使用docker -it xxx /bin/sh进入centos镜像,然后安装了docker,想使用systemctl start docker ...
- git push origin master和git push有什么区别?
1.master是主分支,还可以建一些其他的分支用于开发.2.git push origin master的意思就是上传本地当前分支代码到master分支.git push是上传本地所有分支代码到远程 ...
- 【Git】简单使用
[Git & Github] 首先不能混淆两者的概念.git是一个类似于svn的版本管理工具.其可以在本地建立起针对一个项目的众多维度的版本管理体系,提升了开发的效率. 相对的,我们如果想要和 ...
- (诊断)No module named MySQLdb
启动Keystone同步数据库时提示: -- :: TRACE keystone File , in dbapi -- :: TRACE keystone return __import__('MyS ...
- 将Java web应用部署到Tomcat 及部署到Tomcat根目录 的三种方式
Tomcat作为Servlet/JSP容器(服务器)挺不错的,开源免费,需要知道的是Tomcat是一个Web服务器,其符合Servlet/JSP规范,但是却没有实现所有JavaEE规范,所以我们还是应 ...
- golang获取命令行参数
部署golang项目时难免要通过命令行来设置一些参数,那么在golang中如何操作命令行参数呢?可以使用os库和flag库. 1.golang os库获取命令行参数 os可以通过变量Args来获取命令 ...
- [OpenCV] Samples 16: Decompose and Analyse RGB channels
物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义. #include <iostream> #include <stdio.h> #include &quo ...
- Mac和Windows中常见中文字体的英文名称
我们在给HTML元素设置字体的时候经常会有类似 “ font-family:"微软雅黑", "黑体" ” 这样的写法,尤其是在使用Dreamweaver.Apt ...
- [Cubieboard] 镜像资源汇总
Linaro Server 14.04 (SDCard) 下载:cb2-lubuntu-server-tsd-tfcard-v2.0.img.gz 内核:GNU/Linux 3.4.79 armv7l ...
- css3整理--background-clip
background-clip语法: background-clip : border-box || padding-box || content-box 参数取值: border-box:此值为默认 ...