784. Letter Case Permutation---back tracking

题意: 字母变成大小写,数字不变
Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345"]


S = a1bc, 画出递归数如下:如果是字母 就是一个二叉树, 如果是数字就一个节点。所以本质上是一个二叉树的back tracking.

因为不熟悉 Java 字符串处理的一些function, 一开始写了一个特别丑陋的code:

class Solution {
    public List<String> letterCasePermutation(String S) {
        
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder(S);
        dfs(new StringBuilder(), sb, result,0);
        return result;
    
    }
    
    private void dfs(StringBuilder curResult, StringBuilder S, List<String> result, int index){
        
          if(curResult.length() == S.length()){
              result.add(curResult.toString());
              return;
          }
           
          char c = S.charAt(index);  
          curResult.append(c);
          dfs(curResult,S,result,index+1);
          curResult.setLength(curResult.length()-1);   
        
          if(c>='a' && c<='z'){
              
             char ch = change_char(c, true);  
             S.setCharAt(index,ch);  
             curResult.append(ch);  
             dfs(curResult,S,result,index+1);
             curResult.setLength(curResult.length()-1); 
             S.setCharAt(index,c);  
           }  
        
           else if(c>='A' && c<='Z'){    
              char ch = change_char(c,false);
              S.setCharAt(index,ch); 
              curResult.append(ch);   
              dfs(curResult,S,result,index+1);
              curResult.setLength(curResult.length()-1);
              S.setCharAt(index,c);   
          }      
    }
    
    private char change_char(char c, boolean small){
        if(small){
            return (char)(c + 'A'-'a');
        }
        else return  (char)(c + 'a'-'A');
    }
}

用了Character 类里的function 后的code:

优化到了95%

class Solution {
    public List<String> letterCasePermutation(String S) {
        
        List<String> result = new ArrayList<>();
        dfs(new StringBuilder(), S, result,0);
        return result;
    
    }
    
    private void dfs(StringBuilder curResult, String S, List<String> result, int index){
        
          if(curResult.length() == S.length()){
              result.add(curResult.toString());
              return;
          }
           
          Character c = S.charAt(index); 
        
          if(Character.isLetter(c)){
              // left sub tree
              curResult.append(Character.toLowerCase(c));
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);
              
              //right sub tree
              curResult.append(Character.toUpperCase(c));
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);  
          }
        
          else{ // is number
              curResult.append(c);
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);
          }    
    }
}

 

 

转载于:https://www.cnblogs.com/keepAC/p/9938463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值