zoukankan      html  css  js  c++  java
  • 字符串的排列

    题目描述

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。 
    输入描述:
    输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。


    注意要排序!!&&输入可能有重复!!
    class Solution {
    public:
        vector<string> res;
        void pmu(string str,int begin,int len)
            {
            if(begin==str.size()-1)
                {
                res.push_back(str);
                return;
            }
            sort(str.begin()+begin,str.end());
            for(int i=begin;i<len;i++)
                {
                if(begin!=i&&str[begin]==str[i])
                    continue;
                char tmp=str[begin];
                str[begin]=str[i];
                str[i]=tmp;
                pmu(str,begin+1,len);
                tmp=str[i];
                str[i]=str[begin];
                str[begin]=tmp;
            }
        }
        vector<string> Permutation(string str) {
            if(str.empty())
                return res;
            pmu(str,0,str.size());
            return res;
            //str.push_back(str);
        }
    };
  • 相关阅读:
    单位根反演学习笔记
    省选模拟测试17
    省选模拟测试16
    省选模拟测试15
    省选模拟测试14
    省选模拟测试13
    P4491 [HAOI2018]染色
    省选模拟测试12
    P4389 付公主的背包
    洛谷P3403
  • 原文地址:https://www.cnblogs.com/daocaorenblog/p/5375234.html
Copyright © 2011-2022 走看看