Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c9d93f6

Browse files
next permutation
1 parent 76f9d13 commit c9d93f6

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package medium;
2+
/**
3+
* 31. Next Permutation
4+
5+
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
6+
7+
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
8+
9+
The replacement must be in-place, do not allocate extra memory.
10+
11+
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
12+
1,2,3 → 1,3,2
13+
3,2,1 → 1,2,3
14+
1,1,5 → 1,5,1*/
15+
public class NextPermutation {
16+
/**Leetcode has a very good article to illustrate this problem and with animation: https://leetcode.com/articles/next-permutation/
17+
* 1. if the array is already in decrementing order, then there's no next larger permutation possible.
18+
* 2. if not, start from the end of the array, find the first pair of numbers that break the decrementing order
19+
* 3. then from that index going to the right again, find the element that is closest bigger than this number, swap them
20+
* 4. reverse the right half of this array after this index*/
21+
22+
public void nextPermutation(int[] nums) {
23+
int i = nums.length-2;
24+
while (i >= 0 && nums[i] >= nums[i+1]) {
25+
i--;
26+
}
27+
if (i >= 0) {
28+
int j = nums.length-1;
29+
while (j >= 0 && nums[i] >= nums[j]) {
30+
j--;
31+
}
32+
33+
swap(nums, i, j);
34+
}
35+
36+
reverse(nums, i+1);
37+
}
38+
39+
private void reverse(int[] nums, int start) {
40+
int end = nums.length-1;
41+
while (start <= end){
42+
int tmp = nums[start];
43+
nums[start++] = nums[end];
44+
nums[end--] = tmp;
45+
}
46+
}
47+
48+
private void swap(int[] nums, int i, int j){
49+
int tmp = nums[i];
50+
nums[i] = nums[j];
51+
nums[j] = tmp;
52+
}
53+
54+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../../blob/master/EASY/src/easy/LengthofLastWord.java)|O(n)|O(1)|Easy|
117117
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/HARD/src/hard/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
118118
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution]|||Medium
119+
|31|[Next Permutation](https://leetcode.com/problems/next-permutation)|[Solution](../../blob/master/MEDIUM/src/medium/NextPermutation.java)|O(n)|O(1)|Medium|Array
119120
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)|[Solution](../../blob/master/EASY/src/easy/SwapNodesinPairs.java)|O(n)|O(1)|Easy| Recursion, LinkedList
120121
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../../blob/master/HARD/src/hard/MergeKSortedList.java)|O(n*logk)|O(logk)|Hard|Heap
121122
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[Solution](../../blob/master/MEDIUM/src/medium/GenerateParentheses.java)|TBD|O(n)|Medium|Backtracking

0 commit comments

Comments
 (0)