[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 ...
随机推荐
- Day 7 深copy和浅Copy
dict.fromkeys的用法 1 2 3 4 5 6 7 8 9 10 11 #dict.fromkeys的用法 #例子1 dic = dict.fromkeys([1,2,3],[]) prin ...
- Console命令,让js调试更简单
一.显示信息的命令 console.log("normal"); // 用于输出普通信息 console.info("information"); // 用于输 ...
- 哈夫曼树——c++
哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这个定 ...
- AQS源码泛读,梳理设计流程(jdk8)
一.AQS介绍 AQS(AbstractQueuedSynchronizer)抽象队列同步器,属于多线程编程的基本工具:JDK对其定义得很详细,并提供了多种常用的工具类(重入锁,读写锁,信号量,Cyc ...
- iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象
1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...
- 实验三:分别用for、while和do-while循环语句以及递归方法计算n!,并输出算式
一.用for循环计算n! package for_package; import java.util.*;//导入含有输入类的包 public class for_class { /** * @par ...
- java异常层次机构图
以下内容转自:http://www.cnblogs.com/toSeeMyDream/p/5213610.html 在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.下面的 ...
- 剑指offer四十七之求1+2+3+...+n
一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.思路 1.需利用逻辑与的短路特性实现递归终 ...
- (转)MySQL优化笔记(八)--锁机制超详细解析(锁分类、事务并发、引擎并发控制)
当一个系统访问量上来的时候,不只是数据库性能瓶颈问题了,数据库数据安全也会浮现,这时候合理使用数据库锁机制就显得异常重要了. 原文:http://www.jianshu.com/p/163c96983 ...
- Java之集合(六)PriorityQueue
转载请注明源出处:http://www.cnblogs.com/lighten/p/7299233.html 1.前言 本章介绍队列中的PriorityQueue--优先队列,顾名思义,这是一个可以指 ...