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

    题目描述

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

     1 class Solution {
     2 public:
     3     void Permutation(string str,int begin,int end,vector<string>& result)
     4     {
     5         if (begin > end)
     6         {
     7             result.push_back(str);
     8             return;
     9         }
    10         for (int i = begin ; i <= end ; ++i)
    11         {
    12             if(str[begin] != str[i] || begin == i)
    13             {
    14                 char tem = str[begin];
    15                 str[begin] = str[i];
    16                 str[i] = tem;
    17                 Permutation(str,begin+1,end,result);
    18                 tem = str[begin];
    19                 str[begin] = str[i];
    20                 str[i] = tem;
    21             }
    22         }
    23     }
    24     vector<string> Permutation(string str) {
    25         vector<string> reslut;
    26         if (str.length() == 0)
    27             return reslut;
    28         Permutation(str,0,str.length()-1,reslut);
    29         std::sort(reslut.begin(),reslut.end());
    30         return reslut;
    31     }
    32 };
  • 相关阅读:
    Redundant Connection
    Recover Binary Search Tree
    Min Stack
    *Flatten Binary Tree to Linked List
    Copy List with Random Pointer
    Binary Tree Maximum Path Sum
    Arithmetic Slices
    Integer to English Words
    Unique Email Addresses
    Mycat(水平拆分--分表 全局序列)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5157922.html
Copyright © 2011-2022 走看看