arr = [1,2,3]
, the following are all the permutations of arr
: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]
.arr = [1,2,3]
is [1,3,2]
.arr = [2,3,1]
is [3,1,2]
.arr = [3,2,1]
is [1,2,3]
because [3,2,1]
does not have a lexicographical larger rearrangement.nums
, find the next permutation of nums
.Input: nums = [1,2,3]
Output: [1,3,2]
Input: nums = [3,2,1]
Output: [1,2,3]
Input: nums = [1,1,5]
Output: [1,5,1]
1 <= nums.length <= 100
0 <= nums[i] <= 100
CLASS zcl_algo_nxtpermut DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
* Mandatory declaration
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
TYPES ty_nums TYPE STANDARD TABLE OF i WITH EMPTY KEY.
METHODS getNextPermutation
CHANGING lt_nums TYPE STANDARD TABLE.
METHODS reverseNumbers
CHANGING lt_nums TYPE ty_nums
lv_i TYPE i
lv_j TYPE i.
METHODS swapNumbers
CHANGING lt_nums TYPE ty_nums
lv_i TYPE i
lv_j TYPE i.
ENDCLASS.
CLASS zcl_algo_nxtpermut IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
* lt_nums = VALUE ty_nums( ( 3 ) ( 2 ) ( 1 ) ).
DATA(lt_nums) = VALUE ty_nums( ( 1 ) ( 3 ) ( 5 ) ( 4 ) ( 2 ) ).
* lt_nums = VALUE ty_nums( ( 1 ) ( 2 ) ( 3 ) ).
* calling the method
getNextPermutation( CHANGING lt_nums = lt_nums ).
out->write( |next permutation:------->| ).
out->write( lt_nums ).
FREE lt_nums.
ENDMETHOD.
METHOD getNextPermutation.
DATA(lv_i) = 1.
DATA(lv_j) = 1.
* step: 1
LOOP AT lt_nums ASSIGNING FIELD-SYMBOL(<lf_wa>) STEP -1 FROM lines( lt_nums ) - 1.
IF sy-tabix GE 1 AND <lf_wa> < lt_nums[ sy-tabix + 1 ].
lv_i = lines( lt_nums ) + 1 - sy-tabix.
EXIT.
ENDIF.
ENDLOOP.
UNASSIGN <lf_wa>.
* base condition
IF lv_i = 1.
lv_j = lines( lt_nums ).
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).
ELSE.
* step: 2
LOOP AT lt_nums ASSIGNING <lf_wa> FROM 1 TO lv_i.
IF <lf_wa> > lt_nums[ lv_i ].
lv_j = sy-tabix - 1.
EXIT.
ENDIF.
ENDLOOP.
* step: 3
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).
* step: 4: manipulations
lv_j += 1.
lv_i = lines( lt_nums ).
* step: 5
reverseNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_j
lv_j = lv_i ).
ENDIF.
UNASSIGN <lf_wa>.
FREE: lv_i, lv_j.
ENDMETHOD.
METHOD reverseNumbers.
WHILE lv_i < lv_j.
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).
lv_i += 1.
lv_j -= 1.
ENDWHILE.
ENDMETHOD.
METHOD swapNumbers.
DATA(lv_temp) = 0.
* swapping
lv_temp = lt_nums[ lv_i ].
lt_nums[ lv_i ] = lt_nums[ lv_j ].
lt_nums[ lv_j ] = lv_temp.
FREE lv_temp.
ENDMETHOD.
ENDCLASS.
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# index of the first element that is smaller than the element to its right
lv_index = -1
for i in range(len(nums) - 1, 0, -1):
if nums[i] > nums[i - 1]:
lv_index = i - 1
break
# Base condition
if lv_index == -1:
reverse(nums, 0, len(nums) - 1)
return
j = len(nums) - 1
for i in range(len(nums) - 1, lv_index, -1):
if nums[i] > nums[lv_index]:
j = i
break
# swapping
nums[lv_index], nums[j] = nums[j], nums[lv_index]
# reversing
reverse(nums, lv_index + 1, len(nums) - 1)
# reverse method
def reverse(nums, i, j):
while i < j:
# swapping
nums[i], nums[j] = nums[j], nums[i]
# incrementing
i += 1
j -= 1
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function (nums) {
if ((nums === null) || (nums.length <= 1)) return;
var i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
if (i >= 0) {
var j = nums.length - 1;
while (nums[j] <= nums[i]) j--;
swap_numbers(nums, i, j);
}
/** reversing the right side of the array */
reverse_numbers(nums, i + 1, nums.length - 1);
};
function swap_numbers(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function reverse_numbers(array, i, j) {
while (i < j) swap_numbers(array, i++, j--);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
8 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 | |
3 |