题解 | #字符串的排列#

字符串的排列

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

方法之一next_permutation
CPP提供了内置函数next_permutation实现全排列,但需要对原数组进行排序。

class Solution {
public:
    vector Permutation(string str) {
        string path;
        vector res;
        sort(str.begin(), str.end());
        do{
            for(int i = 0; i < str.size(); i++){
                path += str[i];
            }
            res.emplace_back(path);
            path.erase(path.begin(), path.end());
        }while(next_permutation(str.begin(), str.end()));
        return res;
    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
魔法恐龙:这真得给个机会,面试的时候问问不吃饭78.5h怎么做到的
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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