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. 完成。

  1. class Solution {
  2. public:
  3. void nextPermutation(vector<int>& nums) {
  4. int suffix = nums.size() -;
  5. while(suffix > && nums[suffix] <= nums[suffix - ]){
  6. suffix --;
  7. }
  8. if(suffix == ){
  9. reverse(nums.begin(),nums.end());
  10. return;
  11. }
  12. int pivot = suffix - ;
  13. for(int i = nums.size()-; i >= ; i--){
  14. if(nums[i] > nums[pivot]){
  15. int temp = nums[pivot];
  16. nums[pivot] = nums[i];
  17. nums[i] = temp;
  18. break;
  19. }
  20. }
  21. reverse(begin(nums)+suffix,end(nums));
  22.  
  23. return;
  24. }
  25. };

[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. Day 2 Python 基础数据类型

    2.os.path.join()函数 语法:  os.path.join(path1[,path2[,......]]) 返回值:将多个路径组合后返回 注:第一个绝对路径之前的参数将被忽略 1 2 3 ...

  2. python 进程、线程、协程感悟

    进程: 感觉只是使用Process模块加以使用即可: # -*- coding: utf-8 -*- # data:2019-02-23 21:23 # user:DIY # file:thread_ ...

  3. jzoj5879. 【NOIP2018提高组模拟9.22】电路图 B

    tj:一道好題 看區間操作可以想到線段樹 並聯操作公式:a1∗a2/(a1+a2)a1*a2/(a1+a2)a1∗a2/(a1+a2) 串聯操作公式:a1+a2a1+a2a1+a2 我們發現,一個區間 ...

  4. eclipse常见使用方法

    1.修改字体大小,字符类型(设置为中欧字符) window-preferences-General-Appearance-Colors and Fonts-Basic-Text Font修改 2.展示 ...

  5. JAVA抽象类和抽象方法(abstract)

    一.抽象(abstract)的使用 当父类的某些方法不确定时,可以用abstract关键字来修饰该方法[抽象方法],用abstract来修饰该类[抽象类]. 我们都知道,父类是将子类所共同拥有的属性和 ...

  6. 《JAVA与模式》之建造模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal representation ...

  7. pythonweb框架Flask学习笔记04-模板继承

    # -*- coding:utf-8 -*- from flask import render_template,Flask app=Flask(__name__) @app.route('/hell ...

  8. centos7搭建kafka集群-第一篇

    Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我想对用户 ...

  9. 简单选择排序实现(Java)

    简单选择排序实现 简单选择排序就是通过n-i次关键字之间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i (1 ≤ i ≤ n)个记录交换. 代码实现: public class Selec ...

  10. Java之集合(十五)Set综述

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7427554.html 1.前言 原本按照顺序应该是list.queue然后就是set的讲解,但是因为set的实现 ...