Lintcode190 Next Permutation II solution 题解

【题目描述】

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).

给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列。

如果没有下一个排列,则输出字典序最小的序列。

【题目链接】

www.lintcode.com/en/problem/next-permutation-ii/

【题目解析】

首先找到需要变大的那一位。因为求的是next permutation,所以增大位数要尽量靠右边同时增大的数值要尽量小。具体方法如下:

1) 从数组尾部开始,依次找每一位数离它最近的比它小的数的位置(即要变大的位置),记录下这个数和离它最近的比它小的数的index。每一位数得到的结果都和之前保存的结果做比较,保留index最大的要变大的位置和找到这个位置的数的位置,当要变大的位置相同时,保留数值较小的找到这个位置的数的位置。

2) 遍历完整个数组后,根据得到的结果,将要变大位置的数和找到这个位置的数交换位置,并返回变大的位置

3) 将数组从变大的位置的后一位开始从小到大排序即可。这里因为要求inplace所以用了bubble sort,若可以用额外空间还能用更快的排序方法。

【参考答案】

www.jiuzhang.com/solutions/next-permutation-ii/

原文地址:https://www.cnblogs.com/foxli/p/8364691.html

时间: 2024-10-07 05:02:02

Lintcode190 Next Permutation II solution 题解的相关文章

Lintcode18 Subsets II solution 题解

[题目描述] Given a list of numbers that may has duplicate numbers, return all possible subsets Notice:Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subsets. 给

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

Lintcode16 Permutations II solution 题解

[题目描述] Given a list of numbers with duplicate number in it. Find all unique permutations. 给出一个具有重复数字的列表,找出列表所有不同的排列. [题目链接] http://www.lintcode.com/en/problem/permutations-ii/ [题目解析] 跟 Permutations的解法一样,就是要考虑"去重".先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否

Lintcode70 Binary Tree Level Order Traversal II solution 题解

[题目描述] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) [题目链接] www.lintcode.com/en/problem/binary

Lintcode34 N-Queens II solution 题解

[题目描述] Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. [题目链接] http://www.lintcode.com/en/problem/n-queens-ii/ [题目解析] 这道题跟NQueens的解法完全一样(具

Lintcode54 String to Integer II solution 题解

[题目描述] Implement function atoi to convert a string to an integer.If no valid conversion could be performed, a zero value is returned.If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is re

Lintcode36 Reverse Linked List II solution 题解

[题目描述] Reverse a linked list from position m to n. Notice:Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. 翻转链表中第m个节点到第n个节点的部分 注意:m,n满足1 ≤ m ≤ n ≤ 链表长度 [题目链接] http://www.lintcode.com/en/problem/reverse-linked-list-ii/ [题目解析] 反转

Lintcode207 Interval Sum II solution 题解

[题目描述] Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start,end), return the sum from index start to index end in the given array. For modify(index,value), modify the number

Lintcode364 Trapping Rain Water II solution 题解

[题目描述] Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1x1, compute how much water it is able to trap after raining. 给定n x m非负整数,表示二维的海拔地图,每个单元的面积是1 x 1,计算下雨后能捕到多少水. [题目链接] www.lintcode.com/en/problem