next permutation 类题目 31,556

发布时间 2023-09-10 08:06:20作者: xiaoyongyong
31. Next Permutation
Medium

A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for 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].

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.

Given an array of integers numsfind the next permutation of nums.

The replacement must be in place and use only constant extra memory.

Example 1:

Input: nums = [1,2,3]
Output: [1,3,2]

Example 2:

Input: nums = [3,2,1]
Output: [1,2,3]

Example 3:

Input: nums = [1,1,5]
Output: [1,5,1] 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
class Solution {
    public void nextPermutation(int[] nums) {
        //1.find the first decresing nums[a] < nums[a+1], mark a 
        int A = -1;
        for(int i = nums.length - 1; i >= 1; i--) {
            if(nums[i - 1] < nums[i]) {
              A = i - 1;
              break;
            }
        }
        if(A == -1){
          reverse(nums, 0, nums.length - 1);
          return;
        }
        // 2.find the smallest element greater than nums[a] -> b
        int B = -1;
        for(int i = nums.length - 1; i > A; i--) {
          if(nums[i] > nums[A]) {
            B = i;
            break;
          }
        }
        // 3.swap(a, b)
        swap(nums, A, B);
        // 4.sort after a
        reverse(nums, A + 1, nums.length - 1);
    }
    private void swap(int[] nums, int a, int b) {
      int temp = nums[a];
      nums[a] = nums[b];
      nums[b] = temp;
    }
  
    private void reverse(int[] nums, int start, int end) {
      while(start < end) {
        swap(nums, start++, end--);
      }
    }
}
/**


5 7 6 4
A   B

1.find first increasing from right to left , mark as A
2.find the first element greater than A
3.swap A,B
4.reverse (A+1,)



1.find the first decresing nums[a] < nums[a+1], mark a 
2.find the smallest element greater than nums[a] -> b
3.swap(a, b)
4.sort after a

what if we didnt find it, just reverse()


 */
556. Next Greater Element III
Medium

Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.

Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

Example 1:

Input: n = 12
Output: 21

Example 2:

Input: n = 21
Output: -1

Constraints:

  • 1 <= n <= 231 - 1
class Solution {
    public int nextGreaterElement(int n) {
        //1.convert it to array
        char[] arr = (n + "").toCharArray();

        //2.find the first increasing from right -> left, mark A
        int A = -1;
        for(int i = arr.length - 1; i >=1; i--) {
            if(arr[i] > arr[i - 1]) {
                A = i - 1;
                break;
            }
        }
        //2.1 if not able to find A, return -1;
        if(A < 0) return -1;

        //3.find the smallest element greater than A, mark B
        int B = -1;
        for(int i = arr.length - 1; i >=0; i--) {
            if(arr[i] > arr[A]) {
                B = i;
                break;
            }
        }
        //4.swap A and B
        swap(arr, A, B);

        //5.reverse elements after A
        reverse(arr, A + 1, arr.length - 1);

        //6.return back/
        long result = Long.parseLong(new String(arr));
        return result > Integer.MAX_VALUE ? -1 : (int)result;
    }
    private void swap(char[] arr, int x, int y) {
        char temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }
    private void reverse(char[] arr, int x, int y) {
        while(x < y) {
            swap(arr, x++, y--);
        }
    }
}