zoukankan      html  css  js  c++  java
  • 剑指27:字符串的排列

    题目描述

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串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;
    };

  • 相关阅读:
    ACCP7.0-S2-复习自测-15测试分析
    线程
    多线程下的单例模式
    combobox 属性、事件、方法
    java的多线程总结
    爬虫--登录网页
    shell--字符串是否为空
    python--正则表达式 字符串匹配
    mysql---表所在数据库
    python--日期操作
  • 原文地址:https://www.cnblogs.com/hrnn/p/13429622.html
Copyright © 2011-2022 走看看