31. Next Permutation 返回下一个pumutation序列

[抄题]:

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 and use only constant 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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

完全不知道怎么操作啊:从后往前找递增,再把递减的尾巴整个reverse一遍,变成递增。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

把递减的尾巴整个reverse一遍,变成递增。i和后面交换,确保更加递增。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

原文地址:https://www.cnblogs.com/immiao0319/p/9434211.html

时间: 2024-08-30 14:35:20

31. Next Permutation 返回下一个pumutation序列的相关文章

27.Next Permutation(下一个字典序列)

Level: ??Medium 题目描述: 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

LeetCode 31. 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 replaceme

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically

LeetCode 31 Next Permutation(下一个全排列)

题目链接: https://leetcode.com/problems/next-permutation/?tab=Description Problem :寻找给定int数组的下一个全排列(要求:be in-place) 倒序查找到该数组中第一个满足后面的数字大于前面的数字的下标i (当前下标 i 指向 后面的那个比较大的数) 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 下一个全排列 */ public class S

[C++]LeetCode: 79 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 repla

JS函数:返回下一个元素节点

1 //寻找先一个元素节点的函数,把一个元素的下一个节点(nextSibing)作为参数传给该函数,经过筛选返回元素节点 2 function nextElementSiblings(node){ 3 if(node.nodeType == 1){ 4 return node; 5 } 6 if(node.nextSibling){ 7 return nextElementSiblings(node.nextSibling); 8 } 9 return null; 10 }

T-SQL 返回前一下或下一个值

"前一个"概念意味着需要逻辑排序,但是我们知道,表中的行是不骨顺序的,那我们就需要提供一个等同于"前一个"并能够以T-SQL表达式表述的概念.这种逻辑等效的一个例子是"小于当前值的最大值".这句话,在T-SQL中可以使用一个像下面这样的相关子查询进行表达: SELECT orderid,orderdate,empid,custid, (SELECT MAX(O2.orderid) FROM order AS 02 WHERE 02.orderid

js jquery获取当前元素的兄弟级 上一个 下一个元素 jquery如何获取第一个或最后一个子元素

var chils= s.childNodes;  //得到s的全部子节点 var par=s.parentNode;   //得到s的父节点 var ns=s.nextSbiling;   //获得s的下一个兄弟节点 var ps=s.previousSbiling;  //得到s的上一个兄弟节点 var fc=s.firstChild;   //获得s的第一个子节点 var lc=s.lastChile;   //获得s的最后一个子节点 JS获取节点父级,子级元素 先说一下JS的获取方法,其

jQuery获取元素上一个、下一个、父元素、子元素

jQuery.parent(expr),找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(".class") jQuery.parents(expr),类似于jQuery.parents(expr),但是是查找所有祖先元素,不限于父元素 jQuery.children(expr),返回所有子节点,这个方法只会返回直接的孩子节点,不会返回所有的子孙节点 jQuery.contents