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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【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 ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【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 ...

  5. 【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 ...

  6. 【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 ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. Latex 数学符号表

  2. python 爬虫1

    简单访问有道词典的翻译界面,将页面翻译功能简单呈现 import urllib.request import urllib.parse import json content = input(&quo ...

  3. Android与Dalvik

    自学android的同事说:Android 这个妈蛋!! 当初就应该选择c++/c 来开发.现在为了效率又搞ART.简直是折腾,art在android5.0 的时候就是默认了.前段时间还在学习andr ...

  4. Ubuntu/Windows下利用“HIDAPI”库函数实现与Hid类USB设备通信

    一.背景: 最近在做的一个项目需要使用USB传递数据,对USB理解不是很深,USB的系统驱动编写则更是天方 夜谭,因此将设备配置成HID类设备成为了首选.其最大的好处在于,LINUX/Windows系 ...

  5. vc++6.0各种报错合集(附:VC++6.0调出打印窗口的方法)

    背景: 由于VC++6.0对于现在的我来说,只是一个工具,暂时没有太多的时间分配到这块去深究它,由于不明其原理,因此也只是在此把错误积累下来,以备下次相同错误出现时能快速排除,节省时间. 正文 一.出 ...

  6. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  7. php 通过curl post发送json数据实例

    例1  代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...

  8. tomcat JNDI 设置

    一.在Spring配置文件中的配置   <bean id="dataSource" class="org.springframework.jndi.JndiObje ...

  9. Codeforces 260 C. Boredom

    题目链接:http://codeforces.com/contest/456/problem/C 解题报告:给出一个序列,然后选择其中的一个数 k 删除,删除的同时要把k - 1和k + 1也删除掉, ...

  10. Units Problem: How to read text size as custom attr from xml and set it to TextView in java code

    Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...