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. Android------------UI的控件学习内容

    1. android:gravity="fill_horizontal"  :  文本在显示框中内容显示的位置 2.ToggleButton :  切换按钮 3.RadioGrou ...

  2. nginx 场景业务汇总 (中)

    本文链接:http://www.cnblogs.com/zhenghongxin/p/8906225.html,如果可以,请阅读上篇 <nginx场景业务汇总(初)> (十三)负载均衡 轮 ...

  3. 813. Largest Sum of Averages

    We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...

  4. flask组件之flask_script和flask_migrate的使用

    flask_script的使用 作用: 使用命令启动flask项目 # 在项目的启动文件中 from flask import Flask from settings import Settings ...

  5. webservice gsoap 小记

    参考 http://www.cs.fsu.edu/~engelen/soap.html 1. web service client application > wsdl2h -s -o MyHe ...

  6. webpack快速入门——打包后如何调试

    在配置devtool时,webpack给我们提供了四种选项. source-map:在一个单独文件中产生一个完整且功能完全的文件.这个文件具有最好的source map,但是它会减慢打包速度: che ...

  7. webpack快速入门——实战技巧:webpack优化黑技能

    1.抽离jquery,vue(多个第三方类库抽离) 修改入口文件(webpack.config.js中) entry: { entry: './src/entry.js', jquery:'jquer ...

  8. python 初步认识Flask

    1.简介 flask 问题一:  访问百度的流程? a. 客户端: 发送请求报文,  请求行, 请求头, 请求体 b.服务端: 解析请求的报文, 解析域名, 进行路由匹配分发找到对应的视图函数, 打包 ...

  9. ssh密钥认证排错

    sshd配置文件没问题: 目录权限设置也没问题: 但是 ssh -vvv 提示: debug3: no such identity: /Users/user/.ssh/id_rsa,/Users/us ...

  10. 指定nginx某个目录显示目录结构

    1.修改配置文件/usr/local/nginx/conf/nginx.conf 指定目录,开启autoindex为on. location /study { autoindex on; } 2. 保 ...