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", ...
随机推荐
- ubuntu下使用golang、qml与ubuntu sdk开发桌面应用 (简单示例)
找了很长时间go的gui库,试了gtk,准备试qt的时候发现了这个qml库,试了下很好用. ##准备工作 **1.Go 1.2RC1** go的版本应该不能低于这个,我是在1.2RC发布当天升级后发现 ...
- RMAN正确地删除Archivelog以及设置有备库的归档删除策略
原文链接:http://blog.sina.com.cn/s/blog_64e166580100xks5.html 如何正确地删除Archivelog: Archivelog并不能直接得从OS层直接物 ...
- 多线程开发之二 NSOperation
效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewControll ...
- react跳转url,跳转外链,新页面打开页面
react中实现在js中内部跳转路由,有两种方法. 方法一: import PropTypes from 'prop-types'; export default class Header exten ...
- [Bayes] Understanding Bayes: A Look at the Likelihood
From: https://alexanderetz.com/2015/04/15/understanding-bayes-a-look-at-the-likelihood/ Reading note ...
- 解决VisualStudio无法调试的问题
方法1 方法2
- connect()返回SOCKET_ERROR不一定就是连接失败
connect()用于建立与指定socket的连接. 头文件: #include <sys/socket.h> 函数原型: int connect(int s, const struct ...
- LostRoutes项目日志——敌人精灵Enemy解析
Enemy类在Enemy.js中,类Enemy类继承自PhysicsSprite,以便于可以使用物理引擎中的一些特性. 原版的Enemy.js: var Enemy = cc.PhysicsSprit ...
- TSPL学习笔记(1)
扩展语法(Syntactic extensions) 扩展语法就是通过核心语法或已经定义的扩展语法创建一种新的语法模式. Scheme核心语法模式包括: 顶层定义 常量 变量 过程应用 '(quote ...
- 【!Important】Java线程死锁查看分析方法
一.Jconsole Jconsole是JDK自带的图形化界面工具,使用JDK给我们提过的工具JConsole,可以通过cmd打开命令框然后输入Jconsole打开图形工具 然后点击检测死锁就可以查看 ...