【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
思路:
首先,从后向前找到第一对递增的的数 如 1 4 2 5 7 6 3 中的 5 7, 这表示,7 6 3 是一个连续递减的序列,即这三个数字可以组成的最大的数。 把这三个数翻转,就是 3 6 7即这三个数字可以组成的最小的数字。把5与这三个数字中比它大的最小的数字与它交换变成 1 4 2 6 7 5 3,即下一个较大的数字。注意,像2 3 1 3 3 这样后面比交换点数大一点的数有相同的时候,后面翻转后,交换后面的那个。
class Solution {
public:
void nextPermutation(vector<int> &num)
{
if (num.empty()) return; // in reverse order, find the first number which is in increasing trend (we call it violated number here)
int i;
for (i = num.size()-; i >= ; --i)
{
if (num[i] < num[i+]) break;
} // reverse all the numbers after violated number
reverse(begin(num)+i+, end(num));
// if violated number not found, because we have reversed the whole array, then we are done!
if (i == -) return;
// else binary search find the first number larger than the violated number
auto itr = upper_bound(begin(num)+i+, end(num), num[i]);
// swap them, done!
swap(num[i], *itr);
}
};
我自己写得:思路是先交换,再翻转。比上面的繁琐一点。
void nextPermutation(vector<int> &num) {
if(num.empty()) return;
bool b = false;
int chg1 = , chg2 = ;
int post = num.size() - ;
for(int i = num.size() - ; i >= ; --i)
{
if(num[post] > num[i])
{
b = true;
chg1 = i;
chg2 = post;
break;
}
post = i;
} if(!b)
{
reverse(num.begin(), num.end());
return;
} for(int i = chg1 + ; i < num.size(); i++)
{
if(num[i] > num[chg1] && num[i] <= num[chg2])
chg2 = i;
}
swap(num[chg1], num[chg2]); int l1 = chg1 + ;
int l2 = num.size() - ;
while(l1 < l2)
{
swap(num[l1++], num[l2--]);
}
return;
}
【leetcode】Next Permutation(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- ASP.NET中gridview获取当前行的索引值
在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作.例如,可以获得当前行某一个控件元素:设置某一元素的值等等.下面结合实例介绍几种获得GridView当前行索引值的方法. ...
- JAVA访问权限控制[zhuan]
Java的访问权限控制修饰符,从最大权限到最小权限依次是:public.protected.包访问权限(默认,没有关键字)和private.对于类的访问权限只能是:public和包访问权限(但内部类可 ...
- [译]git add
git add git add命令把工作目录下面的有修改的文件添加的index(staging)里面去. git add告诉Git你想在下次commit的时候把什么文件包含进去. 但是, git ad ...
- Request 传值 遇到的中文乱码问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxxx.aspx.cs&quo ...
- 网站SEO优化之添加Sitemap文件。
Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...
- POJ 2115 C Looooops
扩展GCD...一定要(1L<<k),不然k=31是会出错的 .... C Looooops Time Limit: 1000MS Mem ...
- QT共享库的创建与调用(初级)(附:UI界面不能被改变的其中一个原因)
背景: 最近在做的一个项目其中一部分既是实现PC与下位机的USB通信.windows平台下已经完成,现需移植到linux平台下. 在linux系统中,通过一段时间的工作,设备已被配置成hid类(后续再 ...
- 微信将推指纹支付 "指付通"会与Touch ID整合吗
有消息称微信下一版本将推指纹支付“指付通”,解决手机丢失资金安全的问题(这个应该是针对阿里手机支付的弱点),到时候用户绑定的银行卡进行付款时,不用输入密码只需在专门的支付设备(苹果Touch ID ? ...
- Hadoop之Storm命令
Hadoop之Storm命令 1.storm核心概念 stream--->一列火车 tuple--->一节车厢 数据--->乘客 spout--->始发站 bolt---> ...
- javascript高级程序设计---Event对象二
鼠标事件 事件种类 鼠标事件指与鼠标相关的事件,主要有以下一些. (1)click事件 click事件当用户在Element节点.document节点.window对象上,单击鼠标(或者按下回车键)时 ...