LintCode "Previous Permutation"
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"的更多相关文章
- Lintcode: Previous Permuation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...
- Next Permutation & Previous Permutation
Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...
- leetcode_1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- Previous Permutation
Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...
- lintcode :Permutation Index 排列序号
题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...
- 【leetcode】1053. Previous Permutation With One Swap
题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
随机推荐
- Day04_JAVA语言基础第四天
1.循环(掌握) 1.什么时候使用(理解) 如果我们发现有很多重复内容的时候就要使用循环 2.好处(理解) 让我们的代码看起来更精炼了 3.循环的组成(理解) 1 初始化条件:一般定义的是一个初始变量 ...
- CentOS6 Squid代理服务器的安装与配置
代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息.Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一 ...
- 148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 代码如下: /** * Definition for si ...
- jquery保存用户名和密码到cookie里面
http://blog.sina.com.cn/s/blog_633ad0ae0101guij.html
- 作业:用HTML制作邮箱登陆界面
<body leftmargin="200" rightmargin="200"> <font size="45" > ...
- 团队项目开发中,常见的版本控制有svn,git
团队项目开发中,常见的版本控制有svn,git
- 黑马程序员——JAVA基础之简述面向对象,类,变量,匿名对象
------- android培训.java培训.期待与您交流! ---------- 面向对象: 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将 ...
- activity状态的保存和保持(onRetainNonConfigurationInstance和getLastNonConfigurationInstanc
本文转载于:http://chengbs.iteye.com/blog/1156167 比较onsaveinstancestate() 与 onretainnonconfigurationinstan ...
- sybase参数调整
- Measuring the amount of writes in InnoDB redo logs
Choosing a good InnoDB log file size is key to InnoDB write performance. This can be done by measuri ...