首页 > 其他 > 详细

剑指27字符串的排列

时间:2020-08-01 22:15:29      阅读:120      评论:0      收藏:0      [点我收藏+]

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

输入描述:

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

class Solution {
public:
    vector<string> Permutation(string str) {

        
        if (str.empty())
            return ans;
        chang(str,0,str.size());
        sort(ans.begin(),ans.end());
        auto it=unique(ans.begin(),ans.end());
        ans.erase(it,ans.end());
        return ans;
        
        
    }
    void chang(string &str,int start ,int len){
        if (start ==len)
            ans.push_back(str);
        for (int i=start;i<len;i++){
            swap(str[start],str[i]);
            chang(str,start+1,len);
            swap(str[start],str[i]);
        }
    }
    vector< string> ans;
};

剑指27字符串的排列

原文:https://www.cnblogs.com/hrnn/p/13416259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!