46. Full arrangement
We can borrow the question 31. The next permutation of the nextpermutation function to do, a little modification can be
Be careful to order the nums first.
Class Solution {//Return False public Boolean nextpermutation (int[] nums) {if (nums.length = = 1) When there is no next arrangement {return false; } int p =-1; for (int i = nums.length-2; I >= 0; i--) {if (Nums[i] < Nums[i + 1]) {p = i; Break }} if (P! =-1) {int tmp = nums[p]; int q = nums.length-1; while (Nums[q] <= tmp) {q--; } Nums[p] = Nums[q]; NUMS[Q] = tmp; Reverse (p + 1, nums); } else {//reverse (0, nums); return false; } return true; } public void reverse (int k, int[] nums) {if (k >= nums.length) return; int i = k; int j = nums.length-1; while (I < j) {int tmp = nums[i]; Nums[i] = Nums[j]; NUMS[J] = tmp; i++; j--; }} public LiSt<list<integer>> permute (int[] nums) {arrays.sort (nums); list<list<integer>> ans = new arraylist<> (); do {list<integer> tmp = new arraylist<> (); for (int num:nums) {tmp.add (num); } ans.add (TMP); } while (Nextpermutation (nums)); return ans; }}
[Leetcode] 46. Full Array (Java)