Lintcode51 Previous Permutation solution 题解
来源:互联网 发布:网络自学英语 编辑:程序博客网 时间:2024/06/06 20:31
【题目描述】
Given a list of integers, which denote a permutation.Find the previous permutation in ascending order.
Notice:The list may contains duplicate integers.
给定一个整数数组来表示排列,找出其上一个排列。
注意:排列中可能包含重复的整数
【题目链接】
www.lintcode.com/en/problem/previous-permutation/
【题目解析】
这题就是next permutation的逆过程。首先找到从后往前遍历,直到找到一个数(假设位置为i)比之它前一位数小(即该位到数组的最后是一个递增序列),然后将i到数组最后的所有数按递减排列。然后在这个递增数列里面找到第一个比i-1位置上数小的数,交换两个数的位置即可。如果i=1则说明之前的整个数组都是递增序列,现在整个数组变为递减数列,正好是previous permutation,因此不用再做任何其他操作。
【参考答案】
http://www.jiuzhang.com/solutions/previous-permutation/
阅读全文
0 0
- Lintcode51 Previous Permutation solution 题解
- Lintcode52 Next Permutation solution 题解
- Previous Permutation
- Previous Permutation
- Lintcode - Previous Permutation
- #51 Previous Permutation
- Permutation 题解
- [LeetCode] Permutation Sequence, Solution
- String Permutation java solution
- Lintcode13 strStr solution 题解
- Lintcode15 Permutations solution 题解
- Lintcode17 Subsets solution 题解
- Lintcode92 Backpack solution 题解
- Lintcode109 Triangle solution 题解
- Lintcode111 ClimbingStairs solution 题解
- Lintcode125 Backpack ||solution 题解
- Lintcode129 Rehashing solution 题解
- Lintcode130 Heapify solution 题解
- Java 四种线程池的用法分析
- mac 环境变量配置
- oracle 字符串转多行
- 数据结构-栈-数组实现对头的双栈
- #学志#Ubuntu安装lamp(Apache2+MySQL+PHP)
- Lintcode51 Previous Permutation solution 题解
- LeetCode | Evaluate Reverse Polish Notation
- 4-12 判断奇偶性 (10分)
- caffe 追加新层new layer添加.cu文件问题--无法解析的外部符号
- FreeMarker
- iOS开发学习专题-基础知识(四) NSNumber容器 NSData数据 NSData转换的详细使用方式
- 通过WifiManager可以实现对wifi进行操作,实现wifi自动连接等一些功能
- window命令行直接打开手机的APP应用
- 如何使用命令行在github搭建项目?