题目要求

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 解题报告的更多相关文章

  1. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  2. 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 ...

  3. 【Leetcode_easy】784. Letter Case Permutation

    problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...

  4. [LeetCode&Python] Problem 784. Letter Case Permutation

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

  5. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  6. 784. Letter Case Permutation 字符串中字母的大小写组合

    [抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...

  7. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 784. Letter Case Permutation C++字母大小写全排列

    网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...

  9. 【easy】784. Letter Case Permutation

    Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...

随机推荐

  1. java框架篇---struts之文件上传和下载

    Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...

  2. 在layui layer 弹出层中加载 layui table

    layui.use('table', function(){ var table = layui.table; layer.open({ type : 1, area : [ "600px& ...

  3. AbtestingGateway 分流策略添加

    目录结构分布 我们从GitHub上把它下载后解压出来,有以下5个目录,分别是: admin 管理模块,对策略增删改查等功能 diversion 主模块吧,看源码是匹配redis存储的key doc 文 ...

  4. python 带正则的search 模块

    glob  是python 提供的一个支持正则表达式的查找文件的模块. 实现上采用了os.listdir() 和 fnmatch.fnmatch().  但是没有真的invoking a subshe ...

  5. 怎样利用Heartbeat与Floating IP在Ubuntu 14.04上创建高可用性设置

    提供 ZStack社区 内容简单介绍 Heartbeat是一款开源程序,负责将集群基础设施容量--包括集群成员与消息收发--交付至客户server. Hearbeat在高可用性server基础设施其中 ...

  6. SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装

    SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot  thymeleaf模板页面没提示 SpringBoot t ...

  7. linux命令学习(4):cd命令

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的.所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. 命 ...

  8. mui---自定义页面打开的方向

    在使用MUI做APP的时候,会考虑对页面的打开方向做规定,MUI也给我们提供了很多种页面的打开方式. 具体参考: http://ask.dcloud.net.cn/question/174 MUI做A ...

  9. rest_framework框架

    rest_framework框架的认识 它是基于Django的,帮助我们快速开发符合RESTful规范的接口框架. 一  路由 可以通过路由as_view()传参 根据请求方式的不同执行对应不同的方法 ...

  10. I - Cows

    来源 poj 3348 Your friend to the south is interested in building fences and turning plowshares into sw ...