题解 | #字符串的排列#

字符串的排列

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

#include <string>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return string字符串vector
     */
    //全排列字符串集合
    set<string> res;
    //判断此字符串是否已经用过
    int flag[10];
    void find(string &str,string &temp) {
        if(temp.size()==str.size()) {
            res.insert(temp);
            return;
        }
        for(int i=0;i<str.size();i++) {
            //如果当前字符串已经被使用过,就不能再去加入组合
            if(flag[i]==1)continue;

            //加入临时字符串
            flag[i]=1;
            temp.push_back(str[i]);
            find(str,temp);
            //回溯
            flag[i]=0;
            temp.pop_back();
        }
    }
    vector<string> Permutation(string str) {
        // write code here
        string temp;
        find(str,temp);
        vector<string> strs;
        for(string str:res) {
            strs.push_back(str);
        }
        return strs;
    }
};

全部评论

相关推荐

挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
10-17 17:14
门头沟学院 C++
牛客410039819号:北京地区大多是919和927,这两场挂太多人了
投递华为等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务