首页 > 其他 > 详细

字符串全排列,并去重。

时间:2015-12-13 23:38:09      阅读:295      评论:0      收藏:0      [点我收藏+]
public class TestB {
	
	
	public static void permutation(String str,Set set){
		if(str==null) return;
		int len=str.length();
		if(len==0 || len==1)return;
		 permutation(str.toCharArray(), 0,set);
		
	}

	private static void permutation(char[] str, int i, Set set) {
		 if (i >= str.length)
	            return;
	        if (i == str.length - 1) {
	        //    System.out.println(String.valueOf(str));
	            set.add(String.valueOf(str));
	        } else {
	            for (int j = i; j < str.length; j++) {
	                char temp = str[j];
	                str[j] = str[i];
	                str[i] = temp;
	 
	                permutation(str, i + 1,set);
	 
	                temp = str[j];
	                str[j] = str[i];
	                str[i] = temp;
	            }
	        }
		
	}

	public static void main(String[] args) {
		   Set<String>set=new HashSet<>();
		   permutation("acda",set);
		   for(String s:set){
			   System.out.println(s);
		   }

	}

}

  

字符串全排列,并去重。

原文:http://www.cnblogs.com/mlz-2019/p/5043800.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!