【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 ...
随机推荐
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
- 28-Merge Two Sorted Lists
easy 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 11.13python第一周周末练习
2.请输出你的基本个人信息 3.结合逻辑判断,写一个不同学生分数,输出良好,优秀,分数不及格 循环输出 字符串的替换. 以什么开头startwith 以什么结尾endwith 列表转为字符串 字符串转 ...
- SQLyog连接mysql8报2058错误
连接会话时,报如下错误. 通过网上查解决办法,报这个错误的原因是mysql密码加密方法变了 解决办法: 1.先使用mysql -uroot -p输入密码进去mysql 2.ALTER USER 'ro ...
- 基于python win32setpixel api 实现计算机图形学相关操作
最近读研期间上了计算机可视化的课,老师也对计算机图形学的实现布置了相关的作业.虽然我没有系统地学过图形可视化的课,但是我之前逆向过一些游戏引擎,除了保护驱动之外,因为要做透视,接触过一些计算机图形学的 ...
- JavaScript的数据结构快速学-链表的实现
1-单项链表 function LinkedList() { let Node = function(element) { // 辅助类,表示要添加到链表中的项 this.element = elem ...
- SpringMVC(1):SpringMVC入门
一,MVC 概述 MVC:模型,视图,控制器,是一种软件设计规范,本质是将业务逻辑,数据,显示,分离的方式来编写代码:前后端分离 Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来. ...
- Reactor之发射器(Flux、Mono)转换操作函数
数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...
- eclips 配置一个tomcat,启动多个不同端口的web项目
前提: 记录这个文章是因为在网上查资料,很多都是,用eclips.配置多个tomcat,就像下面图这样配置两个tomcat 去启动不同的web: 运动多个web 项目,设置不同的端口,需要多个tomc ...
- 【Matlab】运算符使用整理 * .* / \ .'
[基本运算符] * :矩阵乘法or数与数相乘 .*:点乘,用于矩阵间对应元素的相乘,或数与数之间,数与矩阵之间的相乘. / :右除.a/b表示矩阵a乘以矩阵b的逆 \ :左除.a\b表示矩阵a的逆乘以 ...