Skip to content

Commit 5f28560

Browse files
committed
31. Next Permutation [M]
1 parent 85831e9 commit 5f28560

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

src/leetcode/_31_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._31_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[] ints = {1, 3, 4, 5, 2};
10+
solution.nextPermutation(ints);
11+
for (int anInt : ints) {
12+
System.out.print(anInt + ",");
13+
}
14+
}
15+
}
16+

src/leetcode/_31_/Solution.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode._31_;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public void nextPermutation(int[] nums) {
7+
this.isDone(nums, 0);
8+
}
9+
10+
private boolean isDone(int[] nums, int begin) {
11+
int end = nums.length;
12+
if (end - begin < 1) {
13+
return true;
14+
}
15+
if (end - begin == 2) {
16+
boolean result = nums[end - 1] > nums[begin];
17+
int temp = nums[end - 1];
18+
nums[end - 1] = nums[begin];
19+
nums[begin] = temp;
20+
return result;
21+
}
22+
if (this.isDone(nums, begin + 1)) {
23+
return true;
24+
} else {
25+
int tmp = begin + 1;
26+
while (tmp < end) {
27+
if (nums[begin] < nums[tmp]) {
28+
int temp = nums[begin];
29+
nums[begin] = nums[tmp];
30+
nums[tmp] = temp;
31+
return true;
32+
}
33+
tmp++;
34+
}
35+
Arrays.sort(nums, begin, end);
36+
return false; // [1,3,5,2,4]
37+
}
38+
}
39+
}

src/leetcode/_31_/solution.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### [31\. Next PermutationCopy for Markdown](https://leetcode.com/problems/next-permutation/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
7+
8+
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
9+
10+
The replacement must be and use only constant extra memory.
11+
12+
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
13+
14+
`1,2,3``1,3,2`
15+
`3,2,1``1,2,3`
16+
`1,1,5``1,5,1`
17+
18+
19+
#### Solution
20+
21+
Language: **Java**
22+
23+
```java
24+
class Solution {
25+
   public void nextPermutation(int[] nums) {
26+
       this.isDone(nums, 0);
27+
  }
28+
29+
   private boolean isDone(int[] nums, int begin) {
30+
       int end = nums.length;
31+
       if (end - begin < 1) {
32+
           return true;
33+
      }
34+
       if (end - begin == 2) {
35+
           boolean result = nums[end - 1] > nums[begin];
36+
           int temp = nums[end - 1];
37+
           nums[end - 1] = nums[begin];
38+
           nums[begin] = temp;
39+
           return result;
40+
      }
41+
       if (this.isDone(nums, begin + 1)) {
42+
           return true;
43+
      } else {
44+
           int tmp = begin + 1;
45+
           while (tmp < end) {
46+
               if (nums[begin] < nums[tmp]) {
47+
                   int temp = nums[begin];
48+
                   nums[begin] = nums[tmp];
49+
                   nums[tmp] = temp;
50+
                   return true;
51+
              }
52+
               tmp++;
53+
          }
54+
           Arrays.sort(nums, begin, end);
55+
           return false;  // [1,3,5,2,4]
56+
      }
57+
  }
58+
}
59+
           while (tmp < end) {
60+
```
61+
![](https://ws2.sinaimg.cn/large/006tKfTcgy1g12aolwb05j31050u00xe.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy