From 2e3ca5156bb30138ec0087715a02738355b3a26b Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Mon, 19 Jul 2021 10:40:58 +0800 Subject: [PATCH] =?UTF-8?q?784:=E5=AD=97=E6=AF=8D=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99=E5=85=A8=E6=8E=92=E5=88=97(=E6=9C=AA=E5=AE=8C?= =?UTF-8?q?=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/LetterCasePermutation.java | 72 +++++++++++++++++++ .../editor/cn/LetterCasePermutation.md | 24 +++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/LetterCasePermutation.java create mode 100644 src/main/java/leetcode/editor/cn/LetterCasePermutation.md diff --git a/src/main/java/leetcode/editor/cn/LetterCasePermutation.java b/src/main/java/leetcode/editor/cn/LetterCasePermutation.java new file mode 100644 index 0000000..01c249d --- /dev/null +++ b/src/main/java/leetcode/editor/cn/LetterCasePermutation.java @@ -0,0 +1,72 @@ +//给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。 +// +// +// +// 示例: +//输入:S = "a1b2" +//输出:["a1b2", "a1B2", "A1b2", "A1B2"] +// +//输入:S = "3z4" +//输出:["3z4", "3Z4"] +// +//输入:S = "12345" +//输出:["12345"] +// +// +// +// +// 提示: +// +// +// S 的长度不超过12。 +// S 仅由数字和字母组成。 +// +// Related Topics 位运算 字符串 回溯 +// 👍 282 👎 0 + +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +//784:字母大小写全排列 +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); + } + } + list = temp; + } + List result = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + result.add(new String(list.get(i))); + } + 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/LetterCasePermutation.md b/src/main/java/leetcode/editor/cn/LetterCasePermutation.md new file mode 100644 index 0000000..ca9c10d --- /dev/null +++ b/src/main/java/leetcode/editor/cn/LetterCasePermutation.md @@ -0,0 +1,24 @@ +

给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。

+ +

 

+ +
示例:
+输入:S = "a1b2"
+输出:["a1b2", "a1B2", "A1b2", "A1B2"]
+
+输入:S = "3z4"
+输出:["3z4", "3Z4"]
+
+输入:S = "12345"
+输出:["12345"]
+
+ +

 

+ +

提示:

+ +
    +
  • S 的长度不超过12
  • +
  • S 仅由数字和字母组成。
  • +
+
Related Topics
  • 位运算
  • 字符串
  • 回溯
  • \n
  • 👍 282
  • 👎 0
  • \ No newline at end of file