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
题目意思:给你一串数,要找到下一个排列,比这个排列大,并且恰好比这个排列大,就是说对于原排列和新排列没法找到另一个排列插在这二者之间。比如1,2,3,值是123,那么下一个排列就是132,你没法找到一个居于123和132之间的排列。
public class Solution { public void nextPermutation(int[] nums) { if (nums == null || nums.length == 0) { return ; } int i = nums.length-2; while (i >= 0 && nums[i] >= nums[i+1]) { i--; } if (i >= 0) { int j = i + 1; while (j < nums.length && nums[j] > nums[i]) { j++; } j--; swap(nums, i, j); } reverse(nums, i+1, nums.length-1); } private void reverse(int[] nums, int i, int j) { // TODO Auto-generated method stub while (i < j) { swap(nums, i,j); i++; j--; } } private void swap(int[] nums, int i, int j) { // TODO Auto-generated method stub int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } }
相关推荐
31.Next_Permutation_下一个排列【LeetCode单题讲解系列】
A C++ function which returns the next lexicographic permutation of characters in a string.
排列_next_permutation1
4 }while(next_permutation(a,a+n)); 下面的代码可产生1~n的全排列 #include #include using namespace std; int main(){ int n; while(scanf("%d",&n)&&n){ int a[1000]; for(int i=0;i<n;i++){ scanf(...
全排列 VC 源代码 全排列 VC 源代码 全排列 VC 源代码
一:next_permutation(start,end,//cmp) 使用默认排序方法:按照字典序从小到大 int arr[3]={1,2,3}; do{ for(int num:arr){ cout<<num<< ; } cout<<endl; }while(next_permutation...
2017年计算机等级考试二级C++辅导:全罗列生成算法:next_permutation .docx
全排列.
Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate ...
DFS搜索案例——寻找全排列。 深度优先遍历图的方法是,从图中某顶点v出发: (1)访问顶点v; (2)依次从v的未被访问的邻接点出发,对图进行深度优先遍历;直至图中和v有路径相通的顶点都被访问;...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for ...31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| ...
lru cache leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 ...Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 Trapping Rain W
Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life ...
NextPermutation 下一排序 完成 DFS 题目 说明 状态 NumIslands 岛屿数量 完成 DivideAndConquer 题目 说明 状态 MaxSubArray 最大子序和 完成 Back Tracing 题目 说明 状态 GenerateParenthesis 括号生成 完成 ...
Permutation 解决方法:掌握排列组合的字典序规律,即可。这个规律找不到,我最后还是直接看答案的。 LeetCode: 581. Shortest Unsorted Continuous Subarray 解决方法:无序列中最大最小值 2018-08-17 19:47 ...
Permutation 算法也可用于迭代地查找数组的排列。 也可用于迭代查找数组的排列。 13 . 14 . 15 . 16 . 17 . Algorithm to find kth largest element from an unsorted array in linear time. (1) If the number of ...
Permutation (M) * -> index 주의, 부등호 하나 틀림 33. Search in Rotated Sorted Array (M) * -> 부등호 주의, 부등호 하나 틀림 34. Find First and Last Position of Element in Sorted Array (M) 35. Search ...
Permutation Inversion of Array (Using Merge Sort) Stock Buy and Sell Rotate Matrix 第3天:(数学) Excel 列号 在 log N 中查找 n^x 在数字的阶乘中计算尾随零 在 Log N 网格中查找 GCD 唯一路径通过 GFG 中的...
nextPermutation 等。 : KMP, RabinKarp : 路径压缩、按秩合并。 : 质数测试、筛选等。 : 阶乘、模阶乘、二项式系数、帕斯卡三角。 : 欧几里得公约数,扩展欧几里得,模拟元。 : create2DArray, create3DArray, ...