LeetCode 784 题解

https://leetcode.com/problems/letter-case-permutation/description/

题目大意:不改变原来字符的位置,输出一个字符串里的字母所有大小写的组合。

解题思路:递归深搜

class Solution {
    List<String> res = new ArrayList<>();
    private void dfs(int x,StringBuilder s)
    {
        if(x==s.length())
        {
            res.add(s.toString());
            return ;
        }
//        System.out.println(s+" "+x);
        char tmp = s.charAt(x);
        if(Character.isLetter(tmp))
        {
            s.setCharAt(x, Character.toLowerCase(tmp));
            dfs(x+1,s);
            s.setCharAt(x, Character.toUpperCase(tmp));
            dfs(x+1,s);
        }
        else
            dfs(x+1,s);
    }
    public List<String> letterCasePermutation(String S) {
        dfs(0,new StringBuilder(S));
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/u011439455/article/details/80492519
今日推荐