C++简单代码/set去重/剪枝/追溯法/10行:

字符串的排列

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

C++简单代码/set去重/剪枝/追溯法/10行:

class Solution {
public:
    set<string> res; //去重
    vector<string> Permutation(string str) {
        if (str.empty()) return {};
        string s;
        cutdown (str, s);
        return vector<string> (res.begin(), res.end());
    }
    void cutdown(string str, string s) {
        if (str.size() == 0) res.insert(s);
        for (int i = 0; i < str.size(); i ++) {
            string temp = str;
            cutdown(temp.erase(i, 1), s + str[i]);
        }
    }
};
全部评论
这样得出来的结果保证一定是字典序的吗
点赞 回复 分享
发布于 2021-05-24 10:40

相关推荐

看起来名字可以很长:笑死 我暑期实习阿里云的意向也被 qq 邮箱放在垃圾箱了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
11
3
分享

创作者周榜

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