码迷,mamicode.com
首页 > 其他好文 > 详细

字符串全排列,并去重。

时间:2015-12-13 23:26:44      阅读:151      评论: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/5043799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!