博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Next Permutation
阅读量:4315 次
发布时间:2019-06-06

本文共 1014 字,大约阅读时间需要 3 分钟。

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,31,3,2

3,2,11,2,3

1,1,51,5,1

public void nextPermutation(int[] nums) {        int length = nums.length;        int i = length-2;        while (i>=0 && nums[i]>=nums[i+1]){            i--;        }        int k=i+1;        if (i>=0){            while (k
nums[i]){ k++; } k--; int temp = nums[k]; nums[k]=nums[i]; nums[i]=temp; } reverse(nums,i+1,nums.length-1); } public void reverse(int[] nums,int i,int j){ while (i

转载于:https://www.cnblogs.com/bingo2-here/p/8109581.html

你可能感兴趣的文章
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>