字符串的排列

输入一个字符数组,打印出该字符数组中字符的所有不重复的排列。

import java.util.ArrayList;
import java.util.List;

public class Main {
	
	static List<String> lstchs = new ArrayList<String>();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char[] chs = {'F','G','J','J','H','K','S','L'};
		lstchs.clear();
		Permutation(chs);
		for(int i = 0; i<lstchs.size();i++) {
			System.out.println(lstchs.get(i).toString());
		}
	}
	
	public static void Permutation(char[] chs) {
		if(chs.length == 0)
			return ;
		Permutation(chs,0,0,chs.length);
	}
	
	public static void Permutation(char[] chs,int pBegin,int pstr,int len) {
		if(pBegin == len) {
			String s = "";
			int i = 0;
			while(i<len) {
				s += chs[i];
				i += 1;
			}
			if(lstchs.contains(s) == false)
				lstchs.add(s);
		}
		else {
			for(int index=pBegin;index<len;index++) {
				char temp = chs[index];
				chs[index] = chs[pBegin];
				chs[pBegin] = temp;
				Permutation(chs,pBegin+1,pstr,len);
				temp = chs[index];
				chs[index] = chs[pBegin];
				chs[pBegin] = temp;
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值