784:字母大小写全排列
This commit is contained in:
parent
0728284b25
commit
5a3bac307a
@ -36,33 +36,37 @@ class LetterCasePermutation{
|
||||
Solution solution = new LetterCasePermutation().new Solution();
|
||||
solution.letterCasePermutation("a1b2");
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<String> letterCasePermutation(String s) {
|
||||
char[] chs = s.toCharArray();
|
||||
List<char[]> list = new ArrayList<>();
|
||||
list.add(chs);
|
||||
for (int i = 0; i < chs.length; i++) {
|
||||
if(Character.isDigit(chs[i])){
|
||||
continue;
|
||||
}
|
||||
List<char[]> temp = new ArrayList<>();
|
||||
for (char[] chList:list) {
|
||||
temp.add(chList);
|
||||
if(chList[i]<'a'){
|
||||
chList[i] += 32;
|
||||
temp.add(chList);
|
||||
}else {
|
||||
chList[i] -= 32;
|
||||
temp.add(chList);
|
||||
}
|
||||
}
|
||||
list = temp;
|
||||
return dfs(s, 0);
|
||||
}
|
||||
|
||||
private List<String> dfs(String str, int index) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (char[] chars : list) {
|
||||
result.add(new String(chars));
|
||||
if (index == str.length() - 1) {
|
||||
String s1 = ("" + str.charAt(index)).toLowerCase();
|
||||
String s2 = ("" + str.charAt(index)).toUpperCase();
|
||||
if (s1.equals(s2)) {
|
||||
result.add(s1);
|
||||
} else {
|
||||
result.add(s1);
|
||||
result.add(s2);
|
||||
}
|
||||
} else {
|
||||
List<String> rest = dfs(str, index + 1);
|
||||
String s1 = ("" + str.charAt(index)).toLowerCase();
|
||||
String s2 = ("" + str.charAt(index)).toUpperCase();
|
||||
for (String r : rest) {
|
||||
if (s1.equals(s2)) {
|
||||
result.add(s1 + r);
|
||||
} else {
|
||||
result.add(s1 + r);
|
||||
result.add(s2 + r);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -47,6 +47,8 @@
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.Node;
|
||||
|
||||
//116:填充每个节点的下一个右侧节点指针
|
||||
public class PopulatingNextRightPointersInEachNode {
|
||||
public static void main(String[] args) {
|
||||
@ -56,28 +58,6 @@ public class PopulatingNextRightPointersInEachNode {
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
public Node next;
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, Node _left, Node _right, Node _next) {
|
||||
val = _val;
|
||||
left = _left;
|
||||
right = _right;
|
||||
next = _next;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public Node connect(Node root) {
|
||||
if (root == null) {
|
||||
|
Loading…
Reference in New Issue
Block a user