LeetCode OJ--Next Permutation *
求一个排列的下一个排列。
1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution{
public:
void nextPermutation(vector<int> &num) {
if(num.size() == )
return; const vector<int>::reverse_iterator rfirst= num.rbegin();
const vector<int>::reverse_iterator rend = num.rend(); auto pivot = next(rfirst);
while(pivot != rend && *pivot >= *prev(pivot))
{
++pivot;
} if(pivot == rend)
{
reverse(rfirst,rend);
return;
}
//find the first num great than pivot
auto change = rfirst;
while(*change<=*pivot)
++change; swap(*change,*pivot);
reverse(rfirst,pivot);
return;
}
}; int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back(); Solution myS;
myS.nextPermutation(num);
return ;
}
LeetCode OJ--Next Permutation *的更多相关文章
- LeetCode OJ 60. Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
随机推荐
- Ansible的使用和模块化深入
Ansible配置 配置文件:/etc/ansible/ansible.cfg [default] 默认配置 inventory = /etc/ansible/hosts主机清单 library = ...
- 编译-LAMP基于fastcgi
前言 最近没更新新篇幅了,今天就来点干活,过多的也不说了下面着手干!干!干! 准备环境 centos7.5 apr-1.6.3.tar.gz apr-util-1.6.1.tar.gz h ...
- GoogleTest 之路1-Generic Build Instructions编译指导总方案
准备工作 为了在你的测试中使用GoogleTest, 你必须让你的编译系统 知道到哪里去寻找GoogleTest 的头文件和源文件. 具体的方法只能依赖于你具体使用的哪种编译系统了,一般来讲这个非常容 ...
- ipvsadm分发MySQL读请求
在MySQL的部署场景中,经常使用HAproxy和ipvs来作为读请求转发的网关.ipvs的好处在于本身不需要daemon的方式来运行,而是直接作为kernel的服务来提供:当ipvs和应用程序服务器 ...
- leetcode-17-BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- PAT Basic 1074
1074 宇宙无敌加法器 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星 ...
- noip2017行记
前奏 写了所有的变量名在linux下测,结果发现并没什么用...听说将所有的变量加上下划线,加上自己的名字作为前缀.. lgj,“感觉今年有网络流,今年要立个flag”,zjr“你咋不上天儿” 在车上 ...
- 【Alpha】Scrum Meeting 5-end
第一天:2019/6/19 前言: 第5次会议在6月19日由PM在教9C-501召开. 总结项目,进行单元测试并进行简单的整合.时长60min. 团队GitHub仓库 仓库连接 1.1 今日完成任务情 ...