Next Permutation
来源:互联网 发布:淘宝买演唱会门票骗局 编辑:程序博客网 时间:2024/06/07 03:57
题目描述:
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
(给你一串数,要找到下一个排列,比这个排列大,并且恰好比这个排列大,
就是说对于原排列和新排列没法找到另一个排列插在这二者之间)。
解析:找规律题,从后向前找到不符合递增的第一个数,然后他后面刚刚找过的递增序列比他大的最小数交换,并且重排列他后面的数组
如果整个数组依次递增,那么说明这是最后一个排列,则将其翻转即可。
public class Next_Permutation {public static int Min(int array[],int index,int num){int low=index;for(int i=index+1;i<array.length;i++){if(array[low]>array[i]&&array[i]>num){low=i;}}return low;}public static void Permutation(int array[]){for(int i=array.length-1;i>=1;i--){if(array[i]>array[i-1]){int low=Min(array,i,array[i-1]);array[i-1]=array[i-1]^array[low];array[low]=array[low]^array[i-1];array[i-1]=array[i-1]^array[low];Arrays.sort(array,i,array.length);break;}if(i==1){Arrays.sort(array);}}System.out.println(Arrays.toString(array));}public static void main(String[] args) {int array[]={2,3,6,5,4,1};Permutation(array);}}
阅读全文
1 0
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Activiti中条件分支根据日期类型进行判断的特殊处理
- 蜀传之单刀赴会
- android 按时间显示图片
- 安卓系统广播大全
- 科研数据采集向爬虫
- Next Permutation
- Struts2开发笔记三
- 感想篇:6.1)写博客的利弊分析:很自私的分享
- JavaFx控件org.controlsfx.control.CheckComboBox缺陷说明
- facealignment-3000fps+seetaface+icplib的配置运行
- keystone
- 第五天
- WEB网站常见受攻击方式及解决办法
- 将csv导入到mysql数据表中