java nextpermutation
public static boolean next_permutation(int[] arr) {
int len = arr.length;
int i = len - 1;
// 1. find largest i where arr[i - 1] < arr[i]
while (i > 0) {
if (arr[i - 1] < arr[i]) break;
i--;
}
if (i <= 0) return false;
// 2. find largest j where arr[i - 1] < arr[j] and j >= i
int j = len - 1;
while (j >= i) {
if (arr[i - 1] < arr[j]) break;
j--;
}
// 3. swap elements between arr[i-1] and arr[j]
swap(i - 1, j, arr);
// 4. reverse elements from i to end of array
len--;
while (i < len) {
swap(i, len, arr);
len--;
i++;
}
return true;
}
public static void swap(int x, int y, int[] arr) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
Also in Java:
- java flood fill
- how to remove all items from alist in java
- java timestamp
- java file download
- how to check if the file has remaining without reading from it java
- bukkit java get max players
- java script num toSting syntax eror
- date to string java
- java list all non directory files in the directory
- demo java file
- java null pointer exception
- java windowbuilder full screen
- query java persistence
- count word in string no matter the delimiter java
- java nested loop
- how to create a button in java
- stack class in java
- java how to make a gui
- java - get open ports
- how to make a loop in java
- adding an element to the end of a linked list java
- java fx display simple rectangle
- loop and save letters in a string java
- linux change java