输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba"
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); char[] c = s.toCharArray(); getFullSequence(c, 0); in.close(); } private static void getFullSequence(char[] c, int key) { if (key == c.length) { System.out.println(String.valueOf(c)); } for (int k = key; k < c.length; k++) { if (IsSwap(c, key, k)) { swap(c, key, k); getFullSequence(c, key + 1); swap(c, key, k); } } } private static boolean IsSwap(char[] c, int k, int key) { for (int m = k; m < key; m++) { if (c[m] == c[key]) { return false; } } return true; } private static void swap(char[] c, int k, int i) { char f; f = c[k]; c[k] = c[i]; c[i] = f; } }
// 全排列问题 深搜 / 回溯#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void dfs(vector<string > &res, string &num, string &cur, vector<bool> &vis) { int n = num.size(); if (cur.size() == n) { res.push_back(cur); return; } for (int i = 0; i<n; i++) { if (vis[i] || (i && num[i - 1] == num[i] && vis[i - 1])) continue; cur.push_back(num[i]); vis[i] = true; dfs(res, num, cur, vis); vis[i] = false; cur.pop_back(); } } vector<string> permute(string& nums) { vector<string > res; string cur; vector<bool> vis(nums.size(), false); sort(nums.begin(), nums.end()); dfs(res, nums, cur, vis); return res; } int main(void) { string s; cin >> s; vector<string> strs = permute(s); for (auto v : strs) cout << v << endl; return 0; }
#include <iostream> using namespace std; void swap(int *x, int *y) { int z; z=*x; *x=*y; *y=z; } void permutation(string str,int len, int k) { if (k==len) { for (int i=0; i<=len; i++) cout << str[i]; cout << endl; } else { for (int indx=k; indx<=len; indx++) { swap(str[indx],str[k]); permutation(str,len,k+1); swap(str[indx],str[k]); } } } int main() { string str; while(cin>>str) { int n=str.length(); permutation(str,n-1,0); } return 0; }递归
//理解不了的就根据代码 举例、画图、分解!理解之后,再根据自己的理解写一遍代码,过程可能需重复多次,加油! class Solution { private: vector<string> res; public: void Permu(string str,int begin) { int sSize=str.size(); if(begin==sSize)//begin指向 当前执行排列操作的字符串的第一个字符(即“名义上的第一位”),直到begin指向字符串的末尾 { res.push_back(str); return; } for(int i=begin;i<sSize;++i) { //判断要交换的字符是否相同,相同就不用交换直接跳过,因为i和begin相等时已经计算过了此种情况,所以i和begin相等时不能跳过 if(i==begin||str[i]!=str[begin]) //等价于if(i!=begin&&str[i]==str[begin]) { swap(str[i],str[begin]);//<algorithm>头文件里的交换函数,用于“名义上的第一位”和后面某一位交换 //将“名义上的第一位”固定住,再对剩下的元素进行排列操作,人脑跑递归时,可以先忽略递归调用,先执行for循环,再针对每次 //for循环的结果,将递归函数看成求解这种情况的一个方法,这个方法又会调用这个方法,直到满足前面的终结条件,就好理解了 Permu(str,begin+1);//不能用++begin和begin++ } } } vector<string> Permutation(string str) { int sSize=str.size(); if(sSize!=0) Permu(str,0); return res; } };
public class Permutations { public static void swap(char[] chs,int i,int j){ char temp = chs[i]; chs[i] = chs[j]; chs[j] = temp; } public static void permutations(char[] chs,int i){ if(i==chs.length){ System.out.println(String.valueOf(chs)); } for(int j=i;j<chs.length;j++){ swap(chs,i,j); permutations(chs,i+1); swap(chs,i,j); } } public static void main(String[] args){ String str = "abc"; char[] chs = str.toCharArray(); permutations(chs,0); } }
- }