Leetcode 784

//这代码可真丑陋,但我学到了两点1:char和string可以无缝互相转换2:char可以直接加减数字进行转换string不行

class Solution {
public:
    vector<string> letterCasePermutation(string S) {
        vector<string> res;
        string add;
        DFS(res,S,0,add);
        return res;
    }
    
    char func(char temp){
        if(temp >= 97&&temp <= 122){
            temp -= 32;
        }
        else if(temp <= 90&&temp >= 65){
            temp += 32;
        }
        return temp;
    }
    
    void DFS(vector<string>& res,string s,int pos,string add){
        if(add.size() == s.size()){
            res.push_back(add);
        }
        else{
            for(int i=pos;i < s.size();i++){
                char t = s[i];   
                if(t >= 48 && t <= 57){
                    add += s[i];
                }
                else{
                    string temp = add;
                    temp += func(t);
                    DFS(res,s,i+1,temp);
                    add += s[i];
                }
            }
        if(add.size() == s.size()){ // 这里又加了个是因为害怕最后一个是数字
            res.push_back(add);
        }
            
        }
    }
};

 好方法:很简洁

class Solution {
public:
    vector<string> letterCasePermutation(string S) {
        vector<string> res;
        helper(S, 0, res);
        return res;
    }
    void helper(string& s, int pos, vector<string>& res) {
        if (pos == s.size()) {
            res.push_back(s);
            return;
        }
        helper(s, pos + 1, res);
        if (s[pos] > '9') {
            s[pos] ^= 32;
            helper(s, pos + 1, res);
        }
    }
};

 

转载于:https://www.cnblogs.com/cunyusup/p/9906009.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值