LeetCode 784. Letter Case Permutation (字母大小写全排列 )

题目标签:Backtracking

  用dfs,只对字母分别递归小写 和 大写,具体看code。

  

 

Java Solution: 

Runtime:  1 ms, faster than 100.00 % 

Memory Usage: 42.7 MB, less than 8.00 %

完成日期:12/15/2019

关键点:dfs

class Solution {
    public List<String> letterCasePermutation(String S) {
        List<String> result = new ArrayList<>();
        
        DFS(result, S.toCharArray(), 0);
        
        return result;
    }
    
    private void DFS(List<String> res, char [] cArr, int pos) {
        if(pos == cArr.length) {
            res.add(new String(cArr));
            return;
        }
            
        if(Character.isLetter(cArr[pos])) {
            // lower case
            cArr[pos] = Character.toLowerCase(cArr[pos]);
            DFS(res, cArr, pos+1);
            
            // upper case
            cArr[pos] = Character.toUpperCase(cArr[pos]); 
            DFS(res, cArr, pos+1);
        } else {
            DFS(res, cArr, pos+1);
        }
    }
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @ 2020-03-16 07:02  Jimmy_Cheng  阅读(172)  评论(0编辑  收藏  举报