GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LeetCode_algorithm/Premute.java at master · rpj911/LeetCode_algorithm Expand file tree/ Copy pathPremute.javaMore file actions 113 lines (83 loc) · 3.29 KB / Copy pathPremute.javaFile metadata and controls113 lines (83 loc) · 3.29 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 package Algorithms; import java.util.ArrayList; import java.util.Arrays; public class Premute { public static void main(String[] args){ int[] number = {2,1,1,3}; //ArrayList<ArrayList<Integer>> result = permute(number); ArrayList<ArrayList<Integer>> result = permuteUnique(number); System.out.println(result.toString()); } public static ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list = new ArrayList<Integer>(); premuteHelp(result, list, num, 0); return result; } public static ArrayList<ArrayList<Integer>> premuteHelp(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] input, int pos){ result.add(new ArrayList<Integer>(list)); for (int i = pos; i < input.length; i ++){ list.add(input[i]); premuteHelp(result, list, input, i+1); list.remove(list.size()-1); } //if (list.size() == input.length){ //} return result; } public static ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list = new ArrayList<Integer>(); // record the numbers which has been accessed. boolean[] numFlag = new boolean[num.length]; for(int i = 0; i < numFlag.length; i++) { numFlag[i] = false; } // sort the array. Arrays.sort(num); permuteUniqueHelp(result, list, num, numFlag); return result; } public static void permuteUniqueHelp(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] num, boolean[] numFlag) { if (list.size() == num.length) { result.add(new ArrayList<Integer>(list)); return; } /* for (int i = 0; i < num.length; i ++){ while (i < num.length - 1 && num[i] == num[i + 1] ) { // skip the duplicate characters. i++; } if (numFlag[i] == false) { list.add(num[i]); numFlag[i] = true; }else{ continue; } permuteUniqueHelp(result, list, num, numFlag); list.remove(list.size() - 1); numFlag[i] = false; } */ for (int i = 0; i < num.length; i ++){ // if numFlag[i-1] has been accessed, we should open the access of all the duplicates if (i > 0 && num[i] == num[i - 1] && numFlag[i-1] == false) { // skip the duplicate characters. continue; } if (numFlag[i] == false) { list.add(num[i]); numFlag[i] = true; }else{ continue; } permuteUniqueHelp(result, list, num, numFlag); list.remove(list.size() - 1); numFlag[i] = false; } return; } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |