面試或筆試中,多次遇到以下4個關於排列組合的手撕算法,這裡做個筆記,方法日後查閱:
1. 無重複元素的數組,求全排列;
2. 有重複元素的數組,求全排列;
3. 無重複元素的數組,求組合【子集】;
4. 有重複元素的數組,求組合;
以上四類題,可以用統一的模板實現,如下所示:
/* *【組合&&排列】 *把一個數組裡的數組合全部列出,比如1和2列出來為1,2,12,21. *這個題目可以擴展成四個: *1.無重複數字的數組,求組合*2.有重複數字的數組,求組合*3.無重複數字的數組,求全排列*4.有重複數字的數組,求全排列*【通用思路(遞歸)】: *定義一個函數:從候選集candicate中選取一個組合prefix *每次從候選集candicate中remove一個數,並加入前綴prefix,打印prefix; *再遞歸從新的candicate中remove下一個數,並加入prefix *【對於重複的控制】 *採用hashset保存prefix,打印之前,判斷hashset中是否包含當前生成的prefix, *沒有則打印,並加入hashset;有則不打印*【組合--》排列】 *只需在打印前加一個判斷:若候選集candicate為空,表示遍歷完一次,生成一個排列,可打印*/package xh.offer.practice;import java.util.Arrays;import java.util.HashSet;import java.util.LinkedList;import java.util.List;public class listAllGroup{ public static void main(String[] args) { String [] array = {"1","2"}; String [] repeate = {"1","2","1"}; listAllGroup test = new listAllGroup(); System.out.println("**********no repeate list*******"); test.listAllNoRepeate(Arrays.asList(array),"");//初始化prefix = "" System.out.println("**********repeate list*******"); HashSet<String> noRepeateSet = new HashSet<String>(); test.listAllRepeate(Arrays.asList(repeate), "", noRepeateSet); System.out.println("**************no repeate premutation********************"); test.premutationNoRepeate(Arrays.asList(array),""); System.out.println("*********************repeate premutation**********************"); HashSet<String> repeateSet = new HashSet<String>(); test.premutationRepeate(Arrays.asList(repeate),"", repeateSet); } //無重複的組合public void listAllNoRepeate(List<String> candicate,String prefix){ if(prefix.length() != 0) System.out.println(prefix);//結果長度不為0,則打印輸出該組合for(int i = 0;i < candicate.size();i++){ //將list轉換成linklist鍊錶,方便操作List<String> tempList = new LinkedList<String>(candicate); //templist減少一個數字,並暫存templist中去除的數字String tempString = (String) tempList.remove(i); //遞歸listAllNoRepeate(tempList,prefix + tempString); } } //有重複的組合,加入hashset public void listAllRepeate(List<String> candicate,String prefix,HashSet<String> res){ if(prefix.length() != 0 && !res.contains(prefix)){ System.out.println(prefix); res.add(prefix); } for(int i = 0;i < candicate.size();i++){ List<String> tempList = new LinkedList<String>(candicate); String tempString = tempList.remove(i); listAllRepeate(tempList, prefix+tempString, res);//遞歸} } //無重複的全排列,加入判斷candicate.size() == 0 public void premutationNoRepeate(List<String> candicate,String prefix){ if(candicate.size() == 0){ System.out.println(prefix); } for(int i = 0;i < candicate.size();i++){ List<String> tempList = new LinkedList<String>(candicate); String tempString = tempList.remove(i); premutationNoRepeate(tempList,prefix+tempString); } } //有重複的全排列,加入hashset輔助判斷輸出public void premutationRepeate(List<String> candicate,String prefix,HashSet<String> res) { if(candicate.size() == 0 && !res.contains(prefix)){ System.out.println(prefix); res.add(prefix); } for(int i = 0;i < candicate.size();i++){ List<String> tempList = new LinkedList<String>(candicate); String tempString = tempList.remove(i); premutationRepeate(tempList, prefix+tempString, res); } } }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。