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

Commit d5a84e9

Browse files
solves nextpermutation
1 parent 542c263 commit d5a84e9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [![Java](assets/java.png)](src/RemoveElement.java) [![Python](assets/python.png)](python/remove_element.py) | |
3737
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | [![Java](assets/java.png)](src/NeedleInHaystack.java) [![Python](assets/python.png)](python/needle_in_haystack.py) | |
3838
| 29 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers) | [![Java](assets/java.png)](src/DivideTwoIntegers.java) | |
39+
| 31 | [Next Permutation](https://leetcode.com/problems/next-permutation) | [![Java](assets/java.png)](src/NextPermutation.java) | |
3940
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | [![Java](assets/java.png)](src/SearchInsertPosition.java) [![Python](assets/python.png)](python/search_insert_position.py) | |
4041
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | [![Java](assets/java.png)](src/CountAndSay.java) [![Python](assets/python.png)](python/count_and_say.py) | |
4142
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |

‎src/NextPermutation.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://leetcode.com/problems/next-permutation
2+
// T: O(|nums|)
3+
// S: O(1)
4+
5+
public class NextPermutation {
6+
public void nextPermutation(int[] nums) {
7+
for (int i = nums.length - 2 ; i >= 0 ; i--) {
8+
if (nums[i] < nums[i + 1]) {
9+
int index = indexOfNextLargest(nums, i + 1, nums[i]);
10+
swap(nums, i, index);
11+
reverse(nums, i + 1, nums.length);
12+
return;
13+
}
14+
}
15+
reverse(nums);
16+
}
17+
18+
private void swap(int[] array, int i, int j) {
19+
int temp = array[i];
20+
array[i] = array[j];
21+
array[j] = temp;
22+
}
23+
24+
private int indexOfNextLargest(int[] array, int start, int x) {
25+
for (int i = start ; i < array.length ; i++) {
26+
if (array[i] <= x) {
27+
return i - 1;
28+
}
29+
}
30+
return array.length - 1;
31+
}
32+
33+
private void reverse(int[] array) {
34+
reverse(array, 0, array.length);
35+
}
36+
37+
private void reverse(int[] array, int start, int end) {
38+
for (int i = start ; i < start + (end - start) / 2 ; i++) {
39+
swap(array, i, end - i + start - 1);
40+
}
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /