diff --git a/src/main/java/leetcode/editor/cn/LetterCasePermutation.java b/src/main/java/leetcode/editor/cn/LetterCasePermutation.java index 8651543..c27849b 100644 --- a/src/main/java/leetcode/editor/cn/LetterCasePermutation.java +++ b/src/main/java/leetcode/editor/cn/LetterCasePermutation.java @@ -30,43 +30,47 @@ import java.util.ArrayList; import java.util.List; //784:字母大小写全排列 -class LetterCasePermutation{ +class LetterCasePermutation { public static void main(String[] args) { //测试代码 Solution solution = new LetterCasePermutation().new Solution(); solution.letterCasePermutation("a1b2"); } + //力扣代码 //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public List letterCasePermutation(String s) { - char[] chs = s.toCharArray(); - List list = new ArrayList<>(); - list.add(chs); - for (int i = 0; i < chs.length; i++) { - if(Character.isDigit(chs[i])){ - continue; - } - List 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); + class Solution { + public List letterCasePermutation(String s) { + return dfs(s, 0); + } + + private List dfs(String str, int index) { + List result = new ArrayList<>(); + 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 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); + } } } - list = temp; + return result; } - List result = new ArrayList<>(); - for (char[] chars : list) { - result.add(new String(chars)); - } - return result; } -} //leetcode submit region end(Prohibit modification and deletion) } \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/PopulatingNextRightPointersInEachNode.java b/src/main/java/leetcode/editor/cn/PopulatingNextRightPointersInEachNode.java index 09a63f6..889df6f 100644 --- a/src/main/java/leetcode/editor/cn/PopulatingNextRightPointersInEachNode.java +++ b/src/main/java/leetcode/editor/cn/PopulatingNextRightPointersInEachNode.java @@ -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) {