31. Next Permutation

31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

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

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

Examples1:
1,2,3 → 1,3,2
Examples2:
3,2,1 → 1,2,3
Examples3:
1,1,5 → 1,5,1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/next-permutation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Think and method:
This question is not an easy question to understand. It may be easier to understand by looking directly at the example. In fact, it is simply a process of rearranging the sequence to find a larger sequence

**1. Violent **
This is the easiest and most straightforward way to think about it, we find every possible arrangement of the list made up of the elements of a given array, and we find a larger arrangement than the one given.
However, this method is too slow, requires too much searching and sorting process, takes a long time, and is too complicated in code implementation, beyond the time limit of the topic.

2. Only one scan is needed
Let’s focus on this.

  1. First, how do we find a larger sequence: find an ascending adjacent element and swap its large number with the decimal before the ascending order to achieve this, and we need to increase the minimum, so obviously we need to look from back to front
  2. After the swap, the ascending element is followed by descending, so we need to rearrange this to be in ascending order
  3. In special cases, if it cannot be found, the whole sequence can be sorted directly in descending order

we can use a more complex examples:
12374653
from right to left
first find ascending element 4 6
then find the element bigger than 4 is 5
swap 4 and 5
->12375643
and resort it
12375346

Time complexity: O(n)
Space complexity: O(1)
Codes:

func nextPermutation(nums []int)  {
    // Find the first ascending sequence from right to left
	i := len(nums) - 2  
	
	for i >= 0 && nums[i] >= nums[i+1] {
		i--
	}

	if i < 0 {
		// the whole interval is dicreasing sequence and directly sort it
		sort.Ints(nums)
	}else {

		j := len(nums)-1
		// Find the first numeric index coordinate from right to left that is larger than the I index value
		for j >= 0 {
		if nums[j] > nums[i]{
		nums[i], nums[j] = nums[j], nums[i]
        break
		}	
	    j--
		}
		
		sort.Ints(nums[i+1:])

	}
	fmt.Println(nums)
}

Test:
Examples1:
1,2,3
在这里插入图片描述

Examples2:
3,2,1
在这里插入图片描述

Examples3:
1,1,5
在这里插入图片描述
Examples in the think and method
12374653
在这里插入图片描述
This question is more like a question that is inclined to the form of thinking. After careful consideration and reference, it seems that no other method has been found. From the perspective of utility, this is a question that needs to be practiced and tried, otherwise it is difficult to find a method on the spot.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值