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) 感觉这两道题主要考察计算排列组合的 ...
随机推荐
- linux 网络联接
安装完进入系统以后需要配置ip 方法一:图形界面直接配置,在system下的prefrecens下面的network下面设置 方法二: 在安装虚拟机的时候选择“bridge networking ”网 ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
- Python编程感悟
从今年起开始学习python,最开始是听得MIT的那门公开课,然后又看了一本入门的书籍.但是总是没有觉得python很好用.可能的原因现在想起来是:IDE始终找不到顺手的,另外R用得太舒服了- any ...
- 关于kinect的安装与配置工作
一 关于kinect的安装与配置工作 首先要注意的是,使用kinect进行开发,目前有两种不同的驱动方案,经测试这两种方案的驱动是不能兼容的,所以请务必选定其中一种(最好是卸载另外一种). 方案一:使 ...
- Java-->多线程复制(文件指针)
--> 这里用到两种方法...其实也不算两种,就一点点不一样而已... ---> Test 测试类 package com.dragon.java.multithreadcopy; imp ...
- matplotlib example
# This file generates an old version of the matplotlib logofrom __future__ import print_function# Ab ...
- easyUI分页显示
struts2: http://www.cnblogs.com/huozhicheng/archive/2011/09/27/2193605.html springMVC http://blog.cs ...
- abbyy cup a
link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...
- HTML DOM参考手册
HTML DOM是HTML Document Object Model(文档对象模型)的缩写,HTML DOM则是专门适用与HTML/XHTML的文档对象模型.熟悉软件开发的人员可以将HTML DOM ...
- Android sdk 镜像服务器资源
大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...