[LeetCode] 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]作为例子。
- 找到一个最长的非递增后缀suffix,如图所示的[5,3,3,0];
- 选择suffix前的数,设为pivot(橙色数字2);
- 从后缀suffix中找到大于pivot的数中的最小数(深蓝数字3);
- 交换第二步和第三步中的两个数;
- 将suffix倒置(相当于从小到大排序);
完成。
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(一种巧妙的解题方法)的更多相关文章
- 【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/domino-a ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...
- 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
随机推荐
- HTML、CSS
表格标签: 表格标签有:<table> <tr> <th> <td> 让内容居中的标签:<center> 按钮标签:<button&g ...
- python web开发c6——阿里云上ubuntu+flask+gunicorn+nginx服务器部署(一)简单测试
简述 Nginx在服务器部署中的作用 请求通过Nginx实现反向代理,将请求提交给代理服务器.本文中只用了一台服务器,所以是代理到本机. gunicorn的作用 作为服务器代码的容器.接收Nginx的 ...
- webservice gsoap 小记
参考 http://www.cs.fsu.edu/~engelen/soap.html 1. web service client application > wsdl2h -s -o MyHe ...
- Sysbench0.5初体验
最近工作中需要测试数据库的OLTP的性能,参考了下MariaDB的benchmark中的测试脚本,发现脚本中已经使用了Sysbench-0.5,可以在这里https://launchpad.net/s ...
- Git 使用流程
# 下载远程仓库到本地 git clone 仓库地址cd 本地仓库文件夹 # 创建本地开发分支并与远程开发分支关联 git checkout -b develop origin/developgit ...
- java程序连接hive数据库遇到的问题
今天,打算学习一下用hadoop做后台,搭建一个网站,首先第一步便是在本机的eclipse中连接到虚拟机上的hive中,原本以为很简单,但是过程很是艰难.特意做总结 首先hive的版本问题就是一个很大 ...
- 【xsy1172】 染色 dp
题目大意:现有$n$条排成一行的木板,每个木板有一个目标颜色.你每次能将一个区间内的木板分别染成它们的目标颜色,而这次染色的代价为这个区间内不同目标颜色的木板的数量的平方.问将全部木板染成目标颜色的最 ...
- POJ 2247
#include<iostream> #include<algorithm> #include<vector> #include<string> #in ...
- python中如何打印某月日历
Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历: import calendar cal = calendar.month(2017, 10) print ("以下 ...
- python实现二叉树的建立以及遍历(递归前序、中序、后序遍历,队栈前序、中序、后序、层次遍历)
#-*- coding:utf-8 -*- class Node: def __init__(self,data): self.data=data self.lchild=None self.rchi ...