(二十)剑指offer之字符串的排列

题目描述:

输入一个字符串,按字典序打印出该字符串中字符的所有排列。

例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba,结果请按字母顺序输出。

输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

class Solution {
public:
    vector<string> Permutation(string str) {
     Permutation(str, str.size(),0);
        return result;    
    }
private:
    vector<string> result;
    bool IsDuplicate(string str, int num1, int num2){
        while(num1 < num2){
            if(str[num1] == str[num2])
                return true;
            num1++;
        }
        return false;
    }
    void Permutation(string str, int size, int begin){
        if(begin == size-1)
            result.push_back(str);
        sort(str.begin()+begin,str.end());//按照增长的顺序输出
        for(int i=begin; i<size; i++){
            if(IsDuplicate(str, begin, i)) continue;
            swap(str[i],str[begin]);
            Permutation(str, size, begin+1);
            swap(str[i],str[begin]);
        }
    }
};

如有建议或其他问题,可随时给我们留言。或者到以下链接:

https://github.com/gaobaoru/code_day

Star/Fork/Push 您的代码,开源仓库需要您的贡献。

请查看Coding 题目网址和收藏Accepted代码仓库,进行coding!!!

全部评论

相关推荐

05-13 00:41
已编辑
北京邮电大学 Java
理性的杰克刷牛客:ai肯定要有的,最好学一下agent方向加一个智能客服什么的进去,并且多加点什么skill,mcp啥的,另外你现在的项目深度有些浅,这些功能都太简单了,而且也不是真正能扛高并发的实现,没有什么太大的亮点,可以去网上找点更有深度的项目。可以先投一些中小厂,有实习经历以后再去大厂,你现在这个大厂可能机会不大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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