首页 > 其他 > 详细

字符串的排列

时间:2019-03-01 10:28:40      阅读:181      评论:0      收藏:0      [点我收藏+]
import java.util.*;
public class Solution {
  
public ArrayList<String> Permutation(String str){   ArrayList<String> ret = new ArrayList<>();   if(str!=null || str.length()>0){   helper(str.toCharArray(),0,ret);   Collections.sort(ret);   }   return ret;   }   private void helper(char[] chars, int pos, ArrayList<String> ret){   if(pos == chars.length-1) ret.add(String.valueOf(chars));   Set<Character> charSet = new HashSet<>();   for(int i=pos; i<chars.length; i++){   if(!charSet.contains(chars[i])){   charSet.add(chars[i]);   swap(chars, i, pos);   helper(chars, pos+1, ret);   swap(chars, pos, i);   }   }   }   private void swap(char[] chars, int i, int j){   char temp = chars[i];   chars[i] = chars[j];   chars[j] = temp;   } }

 

字符串的排列

原文:https://www.cnblogs.com/zhwcs/p/10454656.html

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