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

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


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

    C++:

     1 class Solution {
     2 private:
     3     vector<string> res ;
     4 public:
     5     vector<string> Permutation(string str) {
     6         if (str.empty())
     7             return res ;
     8         sort(str.begin() , str.end()) ;
     9         bool isUsed[15]  = {false};
    10         string temp = "" ;
    11         backtracking(str,isUsed,temp) ;
    12         return res ;
    13     }
    14     
    15     void backtracking(string str , bool isUsed[] , string temp){
    16         if (temp.size() == str.size()){
    17             res.push_back(temp) ;
    18             return ;
    19         }
    20         for(int i = 0 ; i < str.size() ; i++){
    21             if (isUsed[i])
    22                 continue ;
    23             //处理重复字符  比如两个bac1c2  bac2c1 只算其中一种
    24             if (i!=0 && str[i] == str[i-1] && !isUsed[i-1])
    25                 continue ;
    26             isUsed[i] = true ;
    27             temp += str[i] ;
    28             backtracking(str,isUsed,temp) ;
    29             temp.pop_back();
    30             isUsed[i] = false ;
    31         }
    32     }
    33 };
  • 相关阅读:
    es6之更优雅的条件语句
    html 提取 公用部分
    jQuery 新添加元素事件绑定无效
    关于ie6块元素行内元素转换
    git 入门级使用
    vim入门级使用
    git安装配置
    学习使用mac
    Angular常用标记
    npm/bower/brew
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/8991692.html
Copyright © 2011-2022 走看看