首页 > 其他分享 >31. Next Permutation

31. Next Permutation

时间:2022-11-05 21:45:52浏览次数:31  
标签:last nums int 31 length Next second Permutation first

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

在当前序列中,从尾端往前寻找两个相邻元素,前一个记为first,后一个记为second,并且满足first 小于 second。然后再从尾端寻找另一个元素number,如果满足first 小于number,即将第first个元素与number元素对调,并将second元素之后(包括second)的所有元素颠倒排序,即求出下一个序列

 

example:
6,3,4,9,8,7,1
此时 first = 4,second = 9
从尾巴到前找到第一个大于first的数字,就是7
交换4和7,即上面的swap函数,此时序列变成6,3,7,9,8,4,1
再将second=9以及以后的序列重新排序,让其从小到大排序,使得整体最小,即reverse一下(因为此时肯定是递减序列)
得到最终的结果:6,3,7,1,4,8,9

 

public void nextPermutation(int[] nums) {
if (nums.length <= 1) {
return;
}
int i= nums.length-1;
for (; i >= 1 ; i--) {
if (nums[i] > nums[i - 1]) { //nums[i]是9,i是3
break;
}
}
if (i != 0) {
swap(nums,i - 1);
}
reverse(nums,i);
}

private void swap(int[] a,int i) {
for (int j = a.length - 1; j > i; j--) {
if(a[j] > a[i]) { //找到第一个比a[i]大的数,交换
int t = a[j];
a[j] = a[i];
a[i] = t;
break;
}
}
}

private void reverse(int[] a,int i){ //把后面的数字逆序排列
int first = i;
int last = a.length-1;
while (first < last) {
int t = a[first];
a[first] = a[last];
a[last] = t;
first ++;
last --;
}
}

 

标签:last,nums,int,31,length,Next,second,Permutation,first
From: https://www.cnblogs.com/MarkLeeBYR/p/16861394.html

相关文章

  • 「题解报告」[POI2008]PER-Permutation
    「题解报告」[POI2008]PER-Permutation点击查看目录目录「题解报告」[POI2008]PER-Permutation思路代码不理解哪里难了,学过扩卢并且推一下式子基本就是两眼切吧。......
  • CCPC 2022桂林 J.Permutation Puzzle
    模拟赛的时候这道题细节写挂了,硬是调不出来。。。首先想到拓补排序。然后可以发现,正反各跑一次可以获得每个点的取值范围,即上界和下界。(特殊地,对于已知点,其上下界相等且等......
  • $el,$nextTick,$set
    this.$elthis.$elDOM的根元素=>是一个完全唯一的$el直到组件挂载完成(mounted)之前都会是undefined。对于单一根元素的组件,$el将会指向该根元素。this.$nestT......
  • [LeetCode] 2131. Longest Palindrome by Concatenating Two Letter Words
    Youaregivenanarrayofstrings words.Eachelementof words consistsof two lowercaseEnglishletters.Createthe longestpossiblepalindrome bysele......
  • Nextflow系列 入门
    一、Nextflow1、Nextflow介绍Nextflow是西班牙巴塞罗那的生物医学和基因组学研究中心CRG开发的开源workflow引擎。是基于Groovy语言的一种工作流框架,能够大大简化复杂计......
  • B - K-th Number HDU - 6231 (二分 尺取)
    WindowsSource2017中国大学生程序设计竞赛-哈尔滨站题意给一个数组,在所有长度大于等于k的区间内,找出第\(k\)大的数,放到另一个数组中,然后在新数组中找到第M大的数。思......
  • 十分钟上手 Next.js
    十分钟上手Next.js写代码的海怪​腾讯前端工程师​关注他 28人赞同了该文章前言Next.js 已经出来很久了,但是一直没机会看这个框架。以前一......
  • Abp vNext 在控制台输出SQL
    废话不说,直接上代码找到EntityFrameworkCore层,在DbContext类(如BookStoreDbContext)。重写OnConfiguring方法protectedoverridevoidOnConfiguring(DbContextOptionsBuil......
  • Next.js 13 All In One
    Next.js13AllInOnehttps://beta.nextjs.org/docshttps://beta.nextjs.org/docs/app-directory-roadmaphttps://beta.nextjs.org/docs/routing/fundamentals$npx......
  • Next.js Environment Variables All In One
    Next.jsEnvironmentVariablesAllInOne.env.env.localDB_HOST=localhostDB_USER=myuserDB_PASS=mypassword//getStaticProps://pages/index.jsexportas......