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) 感觉这两道题主要考察计算排列组合的 ...
随机推荐
- ci(转)
1 从代码管理器签出源文件 2 修改代码 3 编译代码 4 遇到错误,转到2继续修改直到达到预期 5 运行单元测试,期望所有的测试绿色(通过) 6 单元测试出错,转入2 7 重构代码,按 ...
- python 函数性能分析
1 使用profile分析函数性能示例1, 以profile为例: import profile def profileTest(): Total =1; for i in range(10): To ...
- 简单选择排序算法(C++版)
#include <iostream> using namespace std; /** Simple Select Sort * brief: * Key: * * position: ...
- Ionic基础——侧边栏ion-side-menus 以及ion-tap结合侧边栏详解
一. 侧边栏菜单 : ion-side-menus 侧边栏菜单是一个最多包含三个子容器的元素: 默认情况下,侧边栏菜单将只显示ion-side-menu-content容器的内容. 向左滑动时,将显示 ...
- name值与id值在Js获取元素时的区别
1.适用范围 除base.head.html.script.meta.title标签外,id都可以用:name只适用于select.form.frame.iframe.img.a.input等中. H ...
- ubuntu12.04 修复Grub2
电脑双系统,但是把win7重装了之后,会发现grub坏了,只能进入win7. 遇到过好几次,虽然每次都成功解决问题了,但是都花费了不少时间. 所以,总结一下,基本是从网上找到的方法,有的行不通,有的可 ...
- JS中的自定义属性
<div id="div1" a="a" data-bbb="bbb">div</div> <script&g ...
- 【BZOJ1006】【HNOI2008】神奇的国度
这回没看黄学长的代码,看的是chty的 原题: K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K ...
- 兼容FF 加入收藏夹和设为首页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Uinty3d 镜面反射代码
镜面反射代码 文件名MirrorReflection.cs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...