Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1
From right to left, find the first digit (PartitionNumber) which violate the increase trend.
From right to left, find the first digit which larger than PartitionNumber, call it ChangeNumber.
Swap the PartitionNumber and ChangeNumber.
Reverse all the digit on the right of partition index.
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
publicstaticvoidnextPermutation(int[] nums) {
int i=nums.length-1;
while(i>0 && nums[i-1]>=nums[i]){
i--;
}
i--;
if(i<0) {
reverse(nums,0);
return ;
}
int j=nums.length-1;
while(j>i && nums[j]<=nums[i]){
j--;
}
//swap i,j
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
//reverse the partition after i
reverse(nums, i+1);
return;
}
privatestaticvoidreverse(int[] nums, int i){
int l=i, r=nums.length-1;
while(l<r){
int tmp=nums[l];
nums[l]=nums[r];
nums[r]=tmp;
l++;
r--;
}
return;
}
Permutation
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
DFS Solution
Each digit has n possible values
If containing the same values as previous ones, continue
When reached end level, remove last element from list
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
Solution
Similar to Permutations, we only need to make sure not using the same element twice in the same way
Use Visited[] to keep track of currently used element
After sorted, if previous element is not visited and the same with current, then this will be duplicate permutation
if (visited[i] == 1 || (i != 0 && num[i] == num[i - 1] && visited[i - 1] == 0)){
continue;
}
visited[i] = 1;
list.add(num[i]);
helper(result, list, visited, num);
list.remove(list.size() - 1);
visited[i] = 0;
}
}
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): “123” “132” “213” “231” “312” “321” Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 inclusive.
DFS Solution
Find kth permutation, LTE…
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
publicstatic String getPermutation(int n, int k) {
int[] count={0};
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp = dfs(tmp, n, k, count);
StringBuilder re= new StringBuilder();
for(int i : tmp){
re.append(i);
}
return re.toString();
}
privatestatic ArrayList<Integer> dfs(ArrayList<Integer> tmp, int n , int k, int[] count){