题解 | #字符串的排列#

字符串的排列

http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7

class Solution {
public:
    vector<string> Permutation(string str) {
        vector<string> res;
        string path;
        vector<bool> used(str.size(), false);
        sort(str.begin(), str.end());
        dfs(str, res, path, used);
        return res;
    }
    void dfs(string str,vector<string>& res, string path, vector<bool>& used){
        if(path.size()==str.size()){
            res.push_back(path);
            return;
        }
        for(int i=0; i<str.size(); i++){
            if(i>0 && str[i]==str[i-1] && used[i-1]==true){
                continue;
            }
            if(used[i]==false){
                path.push_back(str[i]);
                used[i] = true;
                dfs(str, res, path, used);
                used[i] = false;
                path.pop_back();
            }
        }
    }
};

全部评论

相关推荐

爱读书的放鸽子能手很...:刷个两端实习,冲春招,流水线什么时候不能去
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务