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);
            }
        }
    };
  • 相关阅读:
    JAVA this
    JAVA static关键字
    子类实例化 super
    TCP/IP概述
    Java多态
    植物大战僵尸:寻找阳光掉落Call调用
    JVM总结+个人整理补充--转
    已知微分方程通解求微分方程
    Redis安装过程中的问题
    对称阵和反对称阵
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9906009.html
Copyright © 2011-2022 走看看