题解 | 字符串的排列

字符串的排列

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

#include <type_traits>
class Solution {
  //和全排列一样的思路
public:
    vector<string> ans;
    vector<string> Permutation(string str) {
        if(str.size() ==0)return {""};

        dp(str, 0);

        return ans;
    }

    void dp(string& str, int index){
        set<char> c;  //因为str中有重复的字符,在进行交换时会把相同的字符多次放到本位,所以用set集合,存储本位已经试过的字符,本位不能再使用了

        if(index == str.size()-1){
            ans.push_back(str);
        }

        for(int i = index; i < str.size(); ++i){
            if( c.count(str[i]) == 0){
                c.insert(str[i]);   //标记本位已经用过
                swap(str[i], str[index]);
                dp(str, index+1);
                swap(str[i], str[index]);
            }
        }

        return;
    }

};

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
鼠鼠第一次实习,啥也不懂一直是自己一个人吃的饭,不会做工作老是被嫌弃,大人的世界是这样的吗?
我是星星我会发亮:好的mt有两种,一种愿意教你的,一种几乎什么活都不给你派让你很闲允许你做自己事情的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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