经典指数          
原因
2009
浏览数
0
收藏数
 

输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba"

     举报   纠错  
 
切换
1 个答案
//递归实现,30行,clean #include #include #include using namespace std; vector result; void permute(string& str, int depth, int n){     if(depth == n){          result.push_back(str);         return ;     }     for(int i = depth; i< n; i++){         swap(str[depth],str[i]);         permute(str, depth+1, n);         swap(str[depth],str[i]);     } } int main(){     string str;     cin>>str;     permute(str, 0, str.size());     for(int i = 0; i < result.size(); i++){         cout << result[i] << endl;     }      return  0; }
 
切换
撰写答案
扫描后移动端查看本题