A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy it over and just reverse the conditions.

class Solution {
public:
/**
* @param nums: An array of integers
* @return: An array of integers that's previous permuation
*/
vector<int> previousPermuation(vector<int> &num) {
size_t len = num.size();
if(len < ) return num; // step 1: find first up-side from back
int i = len - ;
while (i > )
{
if (num[i - ] > num[i]) break;
i--;
} // step 2: find last num smaller than num[i-1]
int j = i;
while (j < len)
{
if (num[j] >= num[i - ]) break;
j++;
}
j--; // step 3: replace num[i-1] and num[j]
if(i > )
std::swap(num[i-], num[j]); // step 4: reverse all after i
std::reverse(num.begin() + i, num.end());
return num;
}
};

LintCode "Previous Permutation"的更多相关文章

  1. Lintcode: Previous Permuation

    Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...

  2. Next Permutation & Previous Permutation

    Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...

  3. leetcode_1053. Previous Permutation With One Swap

    1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...

  4. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  5. Previous Permutation

    Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...

  6. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  7. 【leetcode】1053. Previous Permutation With One Swap

    题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. [OJ] Permutation Index

    LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...

随机推荐

  1. wddm 部署问题解决

    在把wddm部署到一台老的服务上的 windows server 2003 上时遇到了问题,之前也在 windows server 2003 上装过,但是并没有遇到问题,估计和服务器比较老有关系. 问 ...

  2. 解决github push错误The requested URL returned error: 403 Forbidden while accessing

    来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The  ...

  3. ~0u >> 1

    ~ 逐位求反u 后辍为 定义unsigned类型>>右移如在32系统中,连起来就是 将32位的0取反后 右移一位.也就是 int 的最大值 2147482347

  4. App_Code 引起的 ambiguously 问题

    今天遇到了一个这样的问题,一个.net framework 4.0 的web application ,下面有一个App_Code文件夹,里面的一些公共类的build action 是 Compile ...

  5. ZMMR104-预留单批量删除及恢复

    ************************************************************************ Title : ZMMR104 ** Applicat ...

  6. poi 合并单元格 无边框问题

    public void merge(int startrow,int endstartrow,int startColumn,int endColumn){ sht.addMergedRegion(n ...

  7. 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. C++ STL库之vector

    vector直译有"容器"之意,我们可以把它理解成是一个不限长度的数组. 我们可以通过代码进一步理解vector. 示例代码如下: #include <stdio.h> ...

  9. SQL注入测试平台 SQLol -2.SELECT注入测试

    前面,我们已经安装好了SQLol,打开http://localhost/sql/,首先跳转到http://localhost/sql/select.php,我们先从select模块进行测试. 一条完成 ...

  10. 使用GoodFeaturesToTrack进行关键点检测---29

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 关键点:是多个方向上亮度变化强的区域. opencv:版本是2.4. 侦测器:opencv有大量的关键点 ...