Next Permutation

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

题目简单来说,就是给你一个序列,然后你要算出这个序列的下一个序列是什么,排序按照字典序。
这题的基本解题思想我是在这篇文章看到的:
Next lexicographical permutation algorithm
下面简单的讲下我的理解,以[0,1,2,5,3,3,0]作为例子。

  1. 找到一个最长的非递增后缀suffix,如图所示的[5,3,3,0];
  2. 选择suffix前的数,设为pivot(橙色数字2);
  3. 从后缀suffix中找到大于pivot的数中的最小数(深蓝数字3);
  4. 交换第二步和第三步中的两个数;
  5. 将suffix倒置(相当于从小到大排序);
  6. 完成。

 class Solution {
public:
void nextPermutation(vector<int>& nums) {
int suffix = nums.size() -;
while(suffix > && nums[suffix] <= nums[suffix - ]){
suffix --;
}
if(suffix == ){
reverse(nums.begin(),nums.end());
return;
}
int pivot = suffix - ;
for(int i = nums.size()-; i >= ; i--){
if(nums[i] > nums[pivot]){
int temp = nums[pivot];
nums[pivot] = nums[i];
nums[i] = temp;
break;
}
}
reverse(begin(nums)+suffix,end(nums)); return;
}
};

[LeetCode] Next Permutation(一种巧妙的解题方法)的更多相关文章

  1. 【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/domino-a ...

  2. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  3. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  4. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  6. 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...

  7. 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  9. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

随机推荐

  1. Spring 开发第一步(二)

    今天继续学习<Spring in action 3rd>并运行书中的例子,到了第4章aop,是加入一个作为切面的Audience类,将Performer的perform()方法作为切点来进 ...

  2. mysql数据库binlog日志的异地备份

    MySQL数据库的二进制日志binlog记录了对数据库的全量DDL和DML操作,对数据库的point to point灾难恢复起着无法替代的关键作用.因此,基于此类考虑,需要对生产环境产生的binlo ...

  3. BZOJ 1002--[FJOI2007]轮状病毒(高精度)

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 6858  Solved: 3745[Submit][Statu ...

  4. Greedy- 621. Task Scheduler

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  5. xp——极限编程的几个方法

    最近阅读<Head First Java>一书时,看到极限编程(XP)的概念,觉得很有趣,摘抄下来以备后期继续学习. 极限编程(XP)是一种新型的软件开发方法论.他的构想是结合了许多种&q ...

  6. XCode9的新变化

    XCode9已经随着ios11的发布发布了,那么在这个XCode9版本中有哪些变化呢? 1 折叠代码 焦点在方法的实现体的方法名上,按comman键,则整个函数会被框住.用来标志这个方法的起点和终点 ...

  7. POJ 1036

    #include<iostream> #include<algorithm> #define MAXN 205 using namespace std; struct node ...

  8. mysql互为主从实战设置详解及自动化备份(Centos7.2)

    mysql互为主从实战设置详解(Centos7.2) 第一步:mysql配置  my.cnf配置 服务器1 (10.89.10.90) [mysqld]  server-id=1  log-bin=/ ...

  9. Opserver 初探三《服务器数据监控》

    用Opserver 怎么像zabbix一样监控服务器呢,查看github官方说明,Opserver可用于连接任何支持Bosun, Orion, or direct WMI监控数据. Opserver ...

  10. Android activity之间的跳转和数据传递

    1.Activity之间的跳转 并且 传递数据 A Activity进行的操作 Intent intent = new Intent(context, B.class); intent.putExtr ...