zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    };
  • 相关阅读:
    kill一个pthread_test.bin测试程序主线程、子线程退出kernel flow
    signal bit operation
    pthread
    信号发送处理流程
    sdcardfs
    node小贴士03
    node小贴士02
    node小贴士01
    siteserver cms 搜索功能
    语法的高亮显示
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9906009.html
Copyright © 2011-2022 走看看