Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35adcbe

Browse files
add 784. 字母大小写全排列
1 parent 682378a commit 35adcbe

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
6+
7+
8+
9+
示例:
10+
11+
```javascript
12+
输入:S = "a1b2"
13+
输出:["a1b2", "a1B2", "A1b2", "A1B2"]
14+
15+
输入:S = "3z4"
16+
输出:["3z4", "3Z4"]
17+
18+
输入:S = "12345"
19+
输出:["12345"]
20+
21+
```
22+
23+
提示:
24+
25+
```javascript
26+
S 的长度不超过12
27+
S 仅由数字和字母组成。
28+
```
29+
30+
来源:力扣(LeetCode)
31+
链接:https://leetcode-cn.com/problems/letter-case-permutation
32+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
33+
34+
35+
36+
37+
## 解题思路
38+
这道题就是递归操作,没有回溯,是一个挺有意思的题目,在讲解思路之前,我先搬运一下大佬的图解,方便我后续补充。
39+
40+
<a href="https://leetcode-cn.com/problems/letter-case-permutation/solution/shen-du-you-xian-bian-li-hui-su-suan-fa-python-dai/">参考大佬 liweiwei1419 图解</a>
41+
42+
第一步
43+
![](https://img-blog.csdnimg.cn/20200913092114823.png#pic_center)
44+
第二步
45+
![](https://img-blog.csdnimg.cn/20200913092124828.png#pic_center)
46+
第三步
47+
![](https://img-blog.csdnimg.cn/20200913092134397.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
48+
第四步
49+
50+
![](https://img-blog.csdnimg.cn/20200913092143552.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
51+
第五步
52+
![](https://img-blog.csdnimg.cn/20200913092154795.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
53+
第六步
54+
![](https://img-blog.csdnimg.cn/20200913092204897.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
55+
好了,有了上述图解之后(还是感谢大佬的图解,万分感谢orz),我相信明白的已经明白了,如果不明白我继续解释。
56+
57+
此题我们只需要从头往后遍历一遍即可,对于非字母节点,我们只会产生一个分支,而对于字母节点,我们可以产生两个分支,即大写字母和小写字母。(详细请参见下述代码)
58+
59+
于是,我们只要简单搜一遍就可以了。
60+
61+
```javascript
62+
/**
63+
* @param {string} S
64+
* @return {string[]}
65+
*/
66+
var letterCasePermutation = function(S) {
67+
let res = []
68+
let dfs = (t,str) => {
69+
if(t.length === S.length)
70+
return res.push(t)
71+
let ch = str[0]
72+
let nextStr = str.substr(1)
73+
// 当前位置为数字,只有一个分支
74+
if(!isNaN(Number(ch))){
75+
dfs(t+ch,nextStr)
76+
}else{
77+
//当前位置为字母,会产生两个分支
78+
let tmp = ch.toUpperCase()
79+
if(tmp === ch) tmp = ch.toLowerCase()
80+
dfs(t+ch,nextStr)
81+
dfs(t+tmp,nextStr)
82+
}
83+
}
84+
dfs('',S)
85+
return res
86+
};
87+
```
88+
89+
90+
91+
## 最后
92+
文章产出不易,还望各位小伙伴们支持一波!
93+
94+
往期精选:
95+
96+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
97+
98+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
99+
100+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
101+
102+
```javascript
103+
学如逆水行舟,不进则退
104+
```
105+
106+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /