784. 字母大小写全排列
题解: 按题意模拟,DFS遍历string的每个下表字符,并对其按题意操作,遍历完添加到答案。
class Solution {
public List<String> letterCasePermutation(String s) {
char[] chars = s.toCharArray();
List<String> res = new ArrayList<>();
dfs(chars, 0, res);
return res;
}
public void dfs(char[] chars, int i, List<String> res) {
if (i >= chars.length) {
res.add(new String(chars));
return;
}
char ch = chars[i];
// 大写字母
if ('A' <= ch && ch <= 'Z') {
dfs(chars, i + 1, res);
chars[i] = (char) (ch + 32);
dfs(chars, i + 1, res);
}
// 小写字母
else if ('a' <= ch && ch <= 'z') {
dfs(chars, i + 1, res);
chars[i] = (char) (ch - 32);
dfs(chars, i + 1, res);
} else {
dfs(chars, i + 1, res);
}
}
}
标签:784,res,字母,List,char,大小写,chars
From: https://www.cnblogs.com/eiffelzero/p/16840385.html