784. Letter Case Permutation_dfs_搜索

此题题意的意思是,给定一个大小写和数字混合的字符串,输出的字符串必须满足字符串中大写变成小写,小写变成大写,

首先定义一个全局变量来存储答案。因为每一个字符所在的位置输出不变,所以不用循环来遍历只需要递归搜索(u+1)来实现对于下一个字符的判断跟改变。我的代码如下:

class Solution {
public:
    vector<string> ans;
    vector<string> letterCasePermutation(string S) {
        dfs (S,0);
        return ans;
        
    }
    void dfs(string S,int u)
    {
        if(u==S.size())
        {
            ans.push_back(S);
            return ;
        }
        dfs(S,u+1);
        if(S[u]>='A')
        {
            S[u]^=32;//位运算/异或把字符串中的大写改成小写,小写改成大写。具体可以转换成二进制来尝试一下。
            dfs(S,u+1);
        }
    }
};

We have been working on it!!!!!

猜你喜欢

转载自blog.csdn.net/Chenxiaoyu99/article/details/83755457