Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9d93f6

Browse files
next permutation
1 parent76f9d13 commitc9d93f6

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+
packagemedium;
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+
publicclassNextPermutation {
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+
publicvoidnextPermutation(int[]nums) {
23+
inti =nums.length-2;
24+
while (i >=0 &&nums[i] >=nums[i+1]) {
25+
i--;
26+
}
27+
if (i >=0) {
28+
intj =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+
privatevoidreverse(int[]nums,intstart) {
40+
intend =nums.length-1;
41+
while (start <=end){
42+
inttmp =nums[start];
43+
nums[start++] =nums[end];
44+
nums[end--] =tmp;
45+
}
46+
}
47+
48+
privatevoidswap(int[]nums,inti,intj){
49+
inttmp =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)

[8]ページ先頭

©2009-2025 Movatter.jp