http://www.itint5.com/oj/#6

首先,试验的时候要拿5个来试,3,4个都太少了。好久没做所以方法也忘了,是先从后往前找到第一个不合顺序的,然后在后面找到比这个大的最小的来交换,再把后面排序。

#include <algorithm>
#include <vector>
#include <climits>
using namespace std;
bool next_permutation(vector<int> &arr) {
bool has_next = false;
int size = arr.size();
int i = arr.size() - 1;
while (i - 1 >= 0) {
if (arr[i-1] < arr[i]) {
has_next = true;
break;
}
i--;
}
if (!has_next) return false;
// find the min after i-1
int min = INT_MAX;
int minIdx = -1;
for (int j = i; j < size; j++) {
if (arr[j] > arr[i-1] && arr[j] < min) {
min = arr[j];
minIdx = j;
}
}
int tmp = arr[i-1];
arr[i-1] = arr[minIdx];
arr[minIdx] = tmp;
// sort elements from i
sort(arr.begin()+i, arr.end());
return true;
}

  

[itint5]下一个排列的更多相关文章

  1. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. lintcode:next permutation下一个排列

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

  3. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  4. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  5. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. [Swift]LeetCode31. 下一个排列 | Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  8. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. leetcode31题:下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...

随机推荐

  1. wpf DataGrid 双击获取当前行的控件

    <DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top& ...

  2. HMTL笔记——Iframe

    1.以iframe 引入的银行支付界面 不能够弹出插件,但是用户安装了插件才能够去支付. 2.以Iframe引入的页面在引入的界面中的跳转都只能在当前Iframe中完成,如果想要跳出则需要window ...

  3. Linq to sql 接收存储过程返回的多个结果集

    故事前提.......... 一.返回顺序结果集 存储过程实例 CREATE PROCEDURE MultipleResultTypesSequentially AS select * from pr ...

  4. 仿春雨医生 安卓app(android)

    仿春雨医生 安卓app(android) 目前APP处与开发完善中,可过程序自下载更新,如有BUG报错,请联系QQ 131 065 1206 支持安卓(android) .IOS(IPHONE),PA ...

  5. String inputStream file转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  6. sqlite3简单使用

    下载SQLite3 地址:http://www.sqlite.org/download.html 下载好的文档是SQlite3.exe,假如放在D盘. cmd D: D:\>SQlite3.ex ...

  7. 使用c#检测文件正在被那个进程占用

    要检测文件被那个进程占用,需要使用微软提供的工具Handle.exe,这里有微软提供的下载 我们可以在c#中调用Handle.exe 来检测到底哪个进程占用了文件 string fileName = ...

  8. memcached 使用积累

    1.memcahed在windows上的安装 . 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached . 在终端(也即cmd命令界面)下输入 ‘c:\memc ...

  9. python(三)一个文件读写操作的小程序

    我们要实现一个文件读写操作的小程序 首先我们有一个文件 我们要以"============"为界限,每一个角色分割成一个独立的txt文件,按照分割线走的话是分成 xiaoNa_1. ...

  10. linux命令之ps命令

    1.管道 linux命令管道通过|表示.一般在linux命令中|(管道)之前的命令会输出大量的结果,|(管道)之后的命令一般就是带有条件的,只将|前满足条件的结果显示出来. 2.grep命令 grep ...