算法<Next Permutation问题>
来源:互联网 发布:遗传算法图像分割 编辑:程序博客网 时间:2024/05/12 07:09
此问题描述如下:
给定一个数组,首先从后向前找出最长的递减序列的前一个元素,比如数组{1, 5, 8, 4, 7, 6, 5, 3, 1}中,从后向前最长递减序列是{ 7, 6, 5, 3, 1},这个序列的前一个元素就是4,然后再递减序列中找到最后一个比这个数(4)大的数字(就是5),然后将5与4交换,得到新的数组{1, 5, 8, 5, 7, 6, 4, 3, 1},然后将交换过后的递减序列进行翻转,最后得到的结果就是{1 5 8 5 1 3 4 6 7 }。如果数组单调递减的数组,比如说是{4,3,2,1},则直接翻转就可以得到最终的结果{1,2,3,4}。
注意交换的过程中不能使用额外的空间
如图表示就是:
代码实现:
/** * @version 2017/7/21.21:33 */public class NextPermutation { public static void main(String[] args) { int[] nums = {1, 5, 8, 4, 7, 6, 5, 3, 1}; new NextPermutation().nextPermutation(nums); for (int num : nums) { System.out.print(num + " "); } } public void nextPermutation(int[] nums) { if (nums == null || nums.length == 1) { return; } int decreasingElementIndex = getDecreasingElement(nums); if (decreasingElementIndex == -1) { reverse(nums, 0); return; } swap(nums, decreasingElementIndex, getLastLargeElement(nums, decreasingElementIndex)); reverse(nums, decreasingElementIndex + 1); } //获取比第一个数组中从后向前递减的数大的数 public int getLastLargeElement(int[] nums, int start) { int num = nums[start]; int index = start + 1; while (index < nums.length) { if (nums[index] > num) { start++; } index++; } return start; } //从数组的 某一位置开始反转数组 public void reverse(int[] nums, int starIndex) { int endIndex = nums.length - 1; while (starIndex < endIndex) { swap(nums, starIndex, endIndex); starIndex++; endIndex--; } } //从数组的后向前获取第一个递减的数字的下标,比如{1,4,3,2},此时要求的 结果就是0 public int getDecreasingElement(int[] nums) { int lenght = nums.length - 1; while (lenght >= 1) { if (nums[lenght] > nums[lenght - 1]) { return lenght - 1; } lenght--; } return -1; } //交换两个数 public void swap(int[] nums, int i, int j) { if (i != j) { nums[i] = nums[i] ^ nums[j]; nums[j] = nums[i] ^ nums[j]; nums[i] = nums[i] ^ nums[j]; } }}
阅读全文
0 0
- 算法<Next Permutation问题>
- LeetCode算法题目:Next Permutation
- leetcode 题解 || Next Permutation 问题
- Next Permutation问题及解法
- Leetcode之Next Permutation 问题
- LeetCode算法题之Next Permutation
- 算法作业HW23:LeetCode 31 Next Permutation
- 算法系列——Next Permutation
- LeetCode算法题——Next Permutation
- 【LeetCode算法练习(C++)】Next Permutation
- c语言解决the next permutation 问题
- [LeetCode]Next Permutation下一个排列问题
- 【leetcode 排列组合问题】Next Permutation | Permutations | Permutations II | Permutation Sequence
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- 分布式事务在银行
- 深度学习下的医学图像分析 2
- “Kotlin in Action”读书笔记
- netlink详解
- 湖南省第六届大学生计算机程序设计竞赛 战场的数目
- 算法<Next Permutation问题>
- 1056. Mice and Rice (25)
- golang 空指针
- 性能分析、调优的概念及手段
- 基于Docker、Jenkins和Saltstack的持续部署方案
- 2.4 linux存储管理-越界访问
- hive简介
- bzoj2565manacher算法
- Flutter学习之旅(三)----Flutter常见问题FAQ,看完之后不再是Flutter小白