【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/letter-case-permutation/description/
题目描述
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.
Examples:
Input: S = "a1b2"
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
Input: S = "3z4"
Output: ["3z4", "3Z4"]
Input: S = "12345"
Output: ["12345"]
Note:
- S will be a string with length at most 12.
- S will consist only of letters or digits.
题目大意
给了一个字符串S,返回把S中的每个字母分别变成大写和小写的情况下,所有的结果组合。如果不是字母的,需要保留在原地。
解题方法
回溯法
看到这个题,仍然想到了回溯法。这个题要求数字保留,字母分成大小写两种。使用回溯法就是分类成数字和字母,字母再分为大写和小写继续。
要注意的一点是不需要使用for循环了。做39. Combination Sum题目的时候使用for循环的目的是能在任意位置起始求和得到目标。本题不需要从任意位置开始。
Python代码如下:
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
res = []
self.dfs(S, 0, res, '')
return res
def dfs(self, string, index, res, path):
if index == len(string):
res.append(path)
return
else:
if string[index].isalpha():
self.dfs(string, index + 1, res, path + string[index].upper())
self.dfs(string, index + 1, res, path + string[index].lower())
else:
self.dfs(string, index + 1, res, path + string[index])
二刷,重写了一下这个回溯法,可以使用字符串切片,能少了一个index变量。
Python代码如下:
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
res = []
self.dfs(S, res, "")
return res
def dfs(self, S, res, word):
if not S:
res.append(word)
return
if S[0].isalpha():
self.dfs(S[1:], res, word + S[0].upper())
self.dfs(S[1:], res, word + S[0].lower())
else:
self.dfs(S[1:], res, word + S[0])
C++代码如下,由于C++对字符串进行切片操作不方便,所以一般都使用了起始位置的方式实现变相切片。
class Solution {
public:
vector<string> letterCasePermutation(string S) {
vector<string> res;
helper(S, res, {}, 0);
return res;
}
void helper(const string S, vector<string>& res, string path, int start) {
if (start == S.size()) {
res.push_back(path);
return;
}
if (S[start] >= '0' && S[start] <= '9') {
helper(S, res, path + S[start], start + 1);
} else {
helper(S, res, path + (char)toupper(S[start]), start + 1);
helper(S, res, path + (char)tolower(S[start]), start + 1);
}
}
};
循环
递归很简单,但是循环时更高效的实现方式。这个实现同样只用一个res数组,然后遍历S,每次判断这个字符是不是字母,然后哦把这个字母分为大小写,然后再次放入到结果中,更新res就好。
时间复杂度是O(N^2),空间复杂度是O(1)。打败96%的提交。
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
res = [""]
for s in S:
if s.isalpha():
res = [word + j for word in res for j in [s.lower(), s.upper()]]
else:
res = [word + s for word in res]
return res
日期
2018 年 2 月 24 日
2018 年 11 月 10 日 —— 这么快就到双十一了??
2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪
【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)的更多相关文章
- LeetCode 784 Letter Case Permutation 解题报告
题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...
- 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 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 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 ...
随机推荐
- nohup使用
nohup:不挂断运行 在忽略挂起信号的情况下运行给定的命令,以便在注销后命令可以在后台继续运行. 可以这么理解:不挂断的运行,注意并没有后台运行的功能,就是指,用nohup 运行命令可以是命令永远运 ...
- centos下Spin Version 6.3.2及ispin安装(2014.9.17)
centos下Spin Version 6.3.2及ispin安装(2014.9.17) 前言:windos下首先安装虚拟机,再安装linux系统(centos版) 一.本帖来源于官网http://s ...
- fastboot烧写Andriod 以及SD 卡烧写LinuxQT,
EMMC是一种FLASH,SD(TF)卡是另外的一种存储,通过控制拨码开关指引CPU去读EMMC还是SD卡的u-boot文件. u-boot的作用 初始化内存控制区,访问存储器,把内核从存储器读取出来 ...
- Navicat连接Linux系统下的Mysql数据库
1 . 进入Linux机器 , 登录并进入mysql如果没有安装mysql,参照 https://blog.csdn.net/weixin_35353187/article/details/81712 ...
- D3学习-加载本地数据
在加载本地数据时,弄了很久都无法显示出来,后来才知道是要把数据文件和html文件都加载到服务器上面 这样就可以显示出来了,
- Redis数据类型内部编码规则及优化方式
Redis的每个键值都是使用一个redisObject结构体保存的,redisObject的定义如下: typedef struct redisObject { unsigned type:4; un ...
- MyEclipse配置Spring框架(基础篇)
一.新建项目,添加spring的相关jar包等 二.创建相关类以及属性和方法 Student.java package com.yh; public class Student implements ...
- UCI数据库_鸢尾花数据集的读取方式
1. 读取数据的第一种方式 [attrib1,attrib2,attrib3,attrb4,class] = textread('iris.data','%f%f%f%f%s','delimiter' ...
- greeting-150
拿到程序例行检查,可以看出程序是32位的程序 将程序放入ida中进入主函数查看 但是我们将程序运行一次后发现程序还运行了nao的程序 说明程序在中间还引用了nao函数,通过代码审计我们可以很直接的看到 ...
- [BUUCTF]PWN——[BJDCTF 2nd]ydsneedgirlfriend2
[BJDCTF 2nd]ydsneedgirlfriend2 附件 步骤: 例行检查,64位程序,开启了canary和nx 试运行一下程序,看看大概的情况,经典的堆块的布局 64位ida载入,习惯性的 ...